一、权限清单: res/xml/devices_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
二、建立和注册设备管理器的Receiver
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by kdroid on 2016/4/22.
*/
public class SampleDeviceAdminReceiver extends DeviceAdminReceiver{
@Override
public void onDisabled(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "disabled dpm", Toast.LENGTH_SHORT).show();
super.onDisabled(context, intent);
}
@Override
public void onEnabled(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "enabled dpm", Toast.LENGTH_SHORT).show();
super.onEnabled(context, intent);
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "disable dpm request", Toast.LENGTH_SHORT)
.show();
return super.onDisableRequested(context, intent);
}
}
<receiver
android:name=".SampleDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/devices_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
三、建立Manager类
public class PolicyManager {
public static final int DPM_ACTIVATION_REQUEST_CODE = 100;
private Context mContext;
private DevicePolicyManager mDPM;
private ComponentName adminComponent;
public PolicyManager(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
mDPM = (DevicePolicyManager) mContext
.getSystemService(Context.DEVICE_POLICY_SERVICE);
adminComponent = new ComponentName(mContext.getPackageName(),
mContext.getPackageName() + ".SampleDeviceAdminReceiver");
}
public boolean isAdminActive() {
return mDPM.isAdminActive(adminComponent);
}
public ComponentName getAdminComponent() {
return adminComponent;
}
public void disableAdmin() {
mDPM.removeActiveAdmin(adminComponent);
}
}
四、开启和禁用设备管理器Intent的写法
// 会直接跳转到当前应用的 激活/关闭 界面
// this will go directly to the activate/de-activate screen of the app you choose:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(context, SecurityDeviceAdminReceiver.class));
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
context.getString(R.string.settings_device_admin_desc));
activity.startActivity(intent);
// 会跳转到设备管理器列表
// this will go to the list of admin apps
final Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.DeviceAdminSettings"));
activity.startActivity(intent);
开启Intent的写法2
// 会跳转到系统界面
if (!policyManager.isAdminActive()) {
Intent activateDeviceAdmin = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
policyManager.getAdminComponent());
activateDeviceAdmin
.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"After activating admin, you will be able to block application uninstallation.");
startActivityForResult(activateDeviceAdmin,
PolicyManager.DPM_ACTIVATION_REQUEST_CODE);
}
关闭Intent的写法2
// 没有跳转界面
policyManager.disableAdmin()
极端用法
// those are quite risky, so you could first use this method:
Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
final PackageManager packageManager=context.getPackageManager();
final List<ResolveInfo> resolveInfos=packageManager.queryIntentActivities(intent,0);
if(resolveInfos!=null&&!resolveInfos.isEmpty()){
try{
final ResolveInfo resolveInfo=resolveInfos.get(0);
intent=new Intent();
intent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name));
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentNameResult);
context.startActivity(intent);
return true;
}catch(final Exception e){}
}
// you can add try-catch for each of the methods, and if all failed , use:
final Intent intent=new Intent(Settings.ACTION_SECURITY_SETTINGS);
activity.startActivity(intent);