1.创建一个空的接收者:
package com.example.mobilesafe.receiver;
import android.app.admin.DeviceAdminReceiver;
public class AdminReceiver extends DeviceAdminReceiver{
}
2.配置使用到的超级管理员功能:
在res/xml下新建一个xml文件,填入如下代码(需要什么功能填什么功能,不写全):
<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>
3.在清单文件中配置权限的描述,标题,以及组件全限定名.
<!-- 超级管理员权限 -->
<receiver android:name="com.example.mobilesafe.receiver.AdminReceiver"
android:label="@string/sample_device_admin"
android:description="@string/sample_device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/adminpolicy" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<!-- 超级管理员权限 -->
4.使用超级管理员的功能:
//清除数据功能
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.wipeData(0);
abortBroadcast();
//锁屏功能
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.lockNow();
abortBroadcast();
5.弹出激活超级管理员的界面:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);ComponentName cn = new ComponentName(this, AdminReceiver.class);//组件名字intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"可以实现清除数据,锁屏功能");startActivity(intent);
6.移除超级管理员权限:
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.
DEVICE_POLICY_SERVICE
);
ComponentName cn =
new
ComponentName(
this
, AdminReceiver.
class
);
//组件名字
dpm
.removeActiveAdmin(
cn
);//移除操作