配置超级管理员步骤:
1. 自定义Receiver,继承DeviceAdminReceiver
2. 配置manifest
<receiver
android:name=".AdminReceiver"
android:description="@string/sample_device_admin_description"
android:label="@string/sample_device_admin"
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>
3. 添加配置文件@xml/device_admin_sample
<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>
4. 通过代码打开超级管理员权限( 调用下面的方法):
public void openAdmin() {
Intent intent = new Intent
(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName component = new ComponentName(this,
AdminReceiver.class); //组件名,写刚才自定义的广播接收者
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, component);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"打开超级管理员权限,可以一键锁屏,删除数据等");
startActivity(intent);
}
激活了以后就可以使用了:
5. 获取DevicePolicyManager
<span style="font-size:14px;">mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE)</span>
6. 验证是否已经激活设备管理员
<span style="font-size:14px;"> ComponentName component = new ComponentName(this, AdminReceiver.class);
if (mDPM.isAdminActive(component)) {//验证是否自己自定义的组件.
}</span>
7. 锁屏
mDPM.lockNow();//锁屏
mDPM.resetPassword("123", 0);//设置锁屏密码
注意: 必须先打开设置->安全->设备管理器的权限,否则运行崩溃
8. 卸载应用(调用下面的方法):
public void uninstall(View view) {
ComponentName component = new ComponentName(this, AdminReceiver.class);
mDPM.removeActiveAdmin(component);//删除超级管理权限,没有激活的应用才可以卸载
//跳转到卸载页面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + getPackageName()));//需要卸载的程序包名.只要当前手机里有的程序可以卸载.
startActivity(intent);
}
9. 清除数据
mDPM.wipeData(0);//恢复出厂设置
mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除sdcard内容