该包主要是控制一些系统的设置:
在res文件建立xml/my_admin.xml文件:
设置的以上这些属性是会在系统设备管理器界面出现的一些权限设置。
然后是需要自己继承DeviceAdminReceiver来实现自己的广播接接受者:
package com.yfz.broadcast;
import com.yfz.log.Logger;
import android.app.admin.DeviceAdminInfo;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.widget.Toast;
public class AdminReceiver extends DeviceAdminReceiver {
@Override
public DevicePolicyManager getManager(Context context) {
Logger.d("------" + "getManager" + "------");
return super.getManager(context);
}
@Override
public ComponentName getWho(Context context) {
Logger.d("------" + "getWho" + "------");
return super.getWho(context);
}
/**
* 禁用
*/
@Override
public void onDisabled(Context context, Intent intent) {
Logger.d("------" + "onDisabled" + "------");
Toast.makeText(context, "禁用设备管理", Toast.LENGTH_SHORT).show();
super.onDisabled(context, intent);
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
Logger.d("------" + "onDisableRequested" + "------");
return super.onDisableRequested(context, intent);
}
/**
* 激活
*/
@Override
public void onEnabled(Context context, Intent intent) {
Logger.d("------" + "onEnabled" + "------");
Toast.makeText(context, "启动设备管理", Toast.LENGTH_SHORT).show();
super.onEnabled(context, intent);
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
Logger.d("------" + "onPasswordChanged" + "------");
super.onPasswordChanged(context, intent);
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
Logger.d("------" + "onPasswordFailed" + "------");
super.onPasswordFailed(context, intent);
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
Logger.d("------" + "onPasswordSucceeded" + "------");
super.onPasswordSucceeded(context, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Logger.d("------" + "onReceive" + "------");
super.onReceive(context, intent);
}
@Override
public IBinder peekService(Context myContext, Intent service) {
Logger.d("------" + "peekService" + "------");
return super.peekService(myContext, service);
}
}
在父类的DeviceAdminReceiver中已经实现了onReceive:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PASSWORD_CHANGED.equals(action)) {
onPasswordChanged(context, intent);
} else if (ACTION_PASSWORD_FAILED.equals(action)) {
onPasswordFailed(context, intent);
} else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) {
onPasswordSucceeded(context, intent);
} else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) {
onEnabled(context, intent);
} else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) {
CharSequence res = onDisableRequested(context, intent);
if (res != null) {
Bundle extras = getResultExtras(true);
extras.putCharSequence(EXTRA_DISABLE_WARNING, res);
}
} else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) {
onDisabled(context, intent);
} else if (ACTION_PASSWORD_EXPIRING.equals(action)) {
onPasswordExpiring(context, intent);
}
}
在不同条件触发的时候会调用不同的方法,所以子类如果需要做一些响应的操作,可以重写方法来实现
其中
android:permission="android.permission.BIND_DEVICE_ADMIN" 和
是必须的。
android:resource="@xml/my_admin" 对应的就是权限说明文件。
这些做好准备之后,就需要在代码里激活设备管理器了:
//获取设备管理服务
policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
//AdminReceiver 继承自 DeviceAdminReceiver
componentName = new ComponentName(this, AdminReceiver.class);
/**
* 激活设备管理权限
* 成功执行激活时,DeviceAdminReceiver中的 onEnabled 会响应
*/
private void activeManage() {
// 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//权限列表
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------");
startActivityForResult(intent, 0);
}
AndroidManifest.xml文件中 android:description="@string/device_des"是做一些说明的
EXTRA_ADD_EXPLANATION参数也是传一些说明文件的
package com.yfz;
import com.yfz.broadcast.AdminReceiver;
import com.yfz.log.Logger;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Lesson13 extends Activity implements OnClickListener {
private DevicePolicyManager policyManager;
private ComponentName componentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson13);
//获取设备管理服务
policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
//AdminReceiver 继承自 DeviceAdminReceiver
componentName = new ComponentName(this, AdminReceiver.class);
init();
}
private void init() {
Button active = (Button)findViewById(R.id.active);
Button unactive = (Button)findViewById(R.id.unactive);
Button syslock = (Button)findViewById(R.id.syslock);
active.setOnClickListener(this);
unactive.setOnClickListener(this);
syslock.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.active:
activeManage();
break;
case R.id.unactive:
unActiveManage();
break;
case R.id.syslock:
systemLock();
break;
default:
break;
}
}
/**
* 激活设备管理权限
* 成功执行激活时,DeviceAdminReceiver中的 onEnabled 会响应
*/
private void activeManage() {
// 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//权限列表
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------");
startActivityForResult(intent, 0);
}
/**
* 禁用设备管理权限
* 成功执行禁用时,DeviceAdminReceiver中的 onDisabled 会响应
*/
private void unActiveManage() {
Logger.d("------ unActiveManage ------");
boolean active = policyManager.isAdminActive(componentName);
if (active) {
policyManager.removeActiveAdmin(componentName);
}
}
/**
* 调出系统锁
*/
private void systemLock() {
Logger.d("------ Lock Screen ------");
boolean active = policyManager.isAdminActive(componentName);
if (active) {
policyManager.lockNow();
}
}
}