1) 在您的 onCreate 中添加此意图以提供 Device_Admin 权限
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"");
startActivityForResult(intent, RESULT_ENABLE);
2) 在清单中添加接收器类:
<receiver
android:name="com.example.AdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@layout/policies" >
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" >
</action>
</intent-filter>
</meta-data>
</receiver>
3) 添加 Receiver 类扩展 DeviceAdminReceiver
public class AdminReceiver extends DeviceAdminReceiver{
static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(
DeviceAdminReceiver.class.getName(), 0);
}
}
4) 在布局文件夹中添加 XML 文件 (policies.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>
</wipe-data>
</force-lock>
</reset-password>
</watch-login>
</limit-password>
</uses-policies>
</device-admin>