實(shí)現(xiàn)一鍵鎖屏的功能
參照谷歌官方文檔:
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.
直接給出我的總結(jié):
1.要?jiǎng)?chuàng)建一個(gè)類繼承 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>
里面的字符串創(chuàng)建就好了,具體什么意思,等你運(yùn)行起來就一目了然,注意android:resource="@xml/device_admin_sample" 著xml 是一個(gè)文件夾,然后里面的文件具體寫的如下,就是一些功能,比如鎖屏等
<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.啟動(dòng)代碼如下,這里要傳入步驟1創(chuàng)建的 .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);
這個(gè)時(shí)候運(yùn)行項(xiàng)目就可以看到管理員頁面了,但是點(diǎn)擊效果還要代碼實(shí)現(xiàn):
@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);
然后激活了設(shè)備管理員的程序卸載比較麻煩,要進(jìn)入設(shè)置里面的設(shè)備管理卸載。