1、创建特定广播接受者DeviceAdminReceiver
public class MyDeviceAdminReceiver extends DeviceAdminReceiver{
}
2、定义安全策略
@xml/device_admin_sample
<device-admin xmlns:android="http://
schemas.android.com/apk/res/android">
<uses-policies>
<limit-password/>限制最短密码长度
<watch-login/>监视登录次数
<reset-password/>重置密码
<fore-lock/>强制锁屏
<wipe-data/>清除数据
<expire-password/>密码有效期
<encrypted-storage/>加密存储设备
<disable-camera/>禁用照相机
</uses-policies>
</device-admin>
3、清单文件配置
<receiver
android:name="Receiver路径"
android:description="@string/描述"
android:label="@string/标签"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"
</intent-filter>
</receiver>
4、使用超级管理员
// 设备安全策略服务
DevicePolicyManager manager = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
manager.lockNow();// 锁屏
manager.resetPassword("111", 0);// 重置密码
manager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);// 清除数据
5、安全设备管理器激活设置
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// 超级管理员组件名称
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
new ComponentName(this, MyDeviceAdminReceive.class));
intent.putExtra(DevicePolicyManager.ETRA_ADD_EXPLANATION,
"这里是激活界面的说明内容");
startActivity(intent);
// 判断设备是否激活
manager.isAdminActive(new CompontentName(this, MyDeviceAdminReceive.class));
6、取消激活
先判断设备是否激活
manager.removeActiveAdmin(new CompontentName(this, MyDeviceAdminReceive.class));
// 卸载应用
Intent intent = new Intent();
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:"+getPackageName()));
startActivity(intent);