注册一个Receiver
- <receiver
- android:name=“LockReceiver”
- android:description=“@string/app_name”
- android:label=“@string/app_name”
- android:permission=“android.permission.BIND_DEVICE_ADMIN” >
- <meta-data
- android:name=“android.app.device_admin”
- android:resource=“@xml/lock_screen” />
- <intent-filter>
- <action android:name=“android.app.action.DEVICE_ADMIN_ENABLED” />
- </intent-filter>
- </receiver>
配置权限文件lock_screen.xml,将文件放置在/res/xml/下
- <?xml version=“1.0” encoding=“UTF-8”?>
- <device-admin xmlns:android=“http://schemas.android.com/apk/res/android” >
- <uses-policies>
- <!– 强行锁定(这里只需要这个) –>
- <force-lock />
- <!– 清除所有数据(恢复出厂设置) –>
- <wipe-data />
- <!– 重置密码 –>
- <reset-password />
- <!– 限制密码选择 –>
- <limit-password />
- <!– 监控登录尝试 –>
- <watch-login />
- </uses-policies>
- </device-admin>
实现Receiver
- package com.huron.xx;
- import android.app.admin.DeviceAdminReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class LockReceiver extends DeviceAdminReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- super.onReceive(context, intent);
- System.out.println(“onreceiver”);
- }
- @Override
- public void onEnabled(Context context, Intent intent) {
- System.out.println(“激活使用”);
- super.onEnabled(context, intent);
- }
- @Override
- public void onDisabled(Context context, Intent intent) {
- System.out.println(“取消激活”);
- super.onDisabled(context, intent);
- }
- }
获取激活状态
public class AdminActiveWrapper { DevicePolicyManager dpm; ComponentName adminComponent; private Activity context = null; public void init(Activity ctx) { context = ctx; dpm = (DevicePolicyManager) context .getSystemService(Context.DEVICE_POLICY_SERVICE); adminComponent = new ComponentName(context, LockReceiver.class); } public boolean isEnabled(){ return dpm.isAdminActive(adminComponent); } public void checkAndEnableAdmin() { if (!dpm.isAdminActive(adminComponent)) { Intent intent = new Intent( DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, context.getString(R.string.device_admin_hint)); context.startActivityForResult(intent, 0); } } }public class S1Activity extends Activity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); finish(); } DevicePolicyManager dpm; ComponentName adminComponent; void checkAndEnableAdmin() { if (!dpm.isAdminActive(adminComponent)) { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, this.getString(R.string.device_admin_hint)); startActivityForResult(intent, 0); } else { finish(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_s1); dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); adminComponent = new ComponentName(this, LockReceiver.class); PermissionManager.upgradeRootPermission(getPackageCodePath()); checkAndEnableAdmin(); } }<string name="device_admin_hint">删除硬件管理设备</string> <string name="device_admin_cancel">真的要删除该硬件设备管理吗?</string> <string name="device_admin_title">硬件设备管理</string>