1.新建一个子类 继承 DeviceAdminReceiver
public class MyAdmin extends DeviceAdminReceiver {
}
2.到清单文件注册这个广播接受者
<receiver
android:name="com.example.lockscreen.MyAdmin"
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>
然后声明所需权限 (这个对应上面的 receiver-> meta-data -> android:resorece的目录)
<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.开启设备管理员权限
public void startAdmin(View view) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName mDeviceAdmin = new ComponentName(MainActivity.this, MyAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"啊啊啊啊啊");
startActivity(intent);
}
private DevicePolicyManager dpm = null;
在oncreate中
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
接下来进行系列操作
比如锁屏 并重设密码
<span style="white-space:pre"> </span>dpm.lockNow();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>dpm.resetPassword("1234", 0);//android 4.4 密码为4位 否则始终错误 不知道是不是没有设置密码长度的关系
5.关闭授权
(必须要关闭授权才能对软件进行卸载)
ComponentName mDeviceAdmin = new ComponentName(MainActivity.this, MyAdmin.class);//第二个参数写前面继承的广播接受者子类
dpm.removeActiveAdmin(mDeviceAdmin);
详情请查看android官方文档: Admnistrator->Devices Polices
地址: http://developer.android.com/guide/topics/admin/device-admin.html