实现一键锁屏的功能
参照谷歌官方文档:
Creating the manifest
To use the Device Administration API, the application's manifest must include the following:
A subclass of DeviceAdminReceiver
that includes the following:The BIND_DEVICE_ADMIN
permission.
The ability to respond to the ACTION_DEVICE_ADMIN_ENABLED
intent, expressed in the manifest as an intent filter.
A decl aration of security policies used in metadata.
直接给出我的总结:
1.要创建一个类继承 DeviceAdminReceiver
然后,在清单文件中增加
<receiver android:name=".app.DeviceAdminSample$DeviceAdminSampleReceiver"
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/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
里面的字符串创建就好了,具体什么意思,等你运行起来就一目了然,注意android:resource="@xml/device_admin_sample" 着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>
4.启动代码如下,这里要传入步骤1创建的 .class 文件
ComponentName name = new ComponentName(this,LockScrrrrrr.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, name);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.add_admin_extra_app_text));
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
这个时候运行项目就可以看到管理员页面了,但是点击效果还要代码实现:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 10:
if (requestCode== RESULT_OK){
dpm.lockNow();
finish();
}else {
// 反激活 ,要先判断是否激活才能反激活
mDPM.removeActiveAdmin(mDeviceAdminSample);
enableDeviceCapabilitiesArea(false);
}
break;
}
}
其中的 dpm 是 dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
然后激活了设备管理员的程序卸载比较麻烦,要进入设置里面的设备管理卸载。