首先在清单文件中配置如下
<receiver
android:name="com.itheima.lockscreen.MyAdmin" //广播接收者 不是BoradCastReceiver
android:description="@string/sample_device_admin_description" //管理员的描述
android:label="@string/sample_device_admin" //管理员标签 (没什么用 随便写)
android:permission="android.permission.BIND_DEVICE_ADMIN" > //<strong>固定!!!</strong>
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />///<strong>权限的表述文件 从帮助文档中copy</strong>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
可以看到 管理员权限 其实 只是一个有特定功能的广播接收者
@xml/device_admin_sample文件如下 主要是列出了所需的权限
<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>
管理员的功能的由DevicePolicyManager对象来实现
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE)
管理员权限 作为特殊的广播接收者 需要我们 手动激活
//创建一个<strong>添加管理员行为</strong>的Intent
Intent intent = new Intent(<strong>DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN</strong>);
//<strong>声明myadmin组件</strong>
ComponentName myAdmin= new ComponentName(this,MyAdmin.class);
//相当于 在
//描述内容 显示在获得管理员权限窗口
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"哥们开启我可以一键锁屏,你的按钮就不会经常失灵");
//启动了DevicePolicyManager 的添加管理员行为
startActivity(intent);
最后 在DevicePolicyManager 中激活管理员权限
dpm.isAdminActive(myAdmin);
dpm.lockNow();锁屏api
<receiver
android:name="com.itheima.lockscreen.MyAdmin" //广播接收者 不是BoradCastReceiver
android:description="@string/sample_device_admin_description" //管理员的描述
android:label="@string/sample_device_admin" //管理员标签 (没什么用 随便写)
android:permission="android.permission.BIND_DEVICE_ADMIN" > //<strong>固定!!!</strong>
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />///<strong>权限的表述文件 从帮助文档中copy</strong>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
可以看到 管理员权限 其实 只是一个有特定功能的广播接收者
@xml/device_admin_sample文件如下 主要是列出了所需的权限
<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>
管理员的功能的由DevicePolicyManager对象来实现
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE)
管理员权限 作为特殊的广播接收者 需要我们 手动激活
//创建一个<strong>添加管理员行为</strong>的Intent
Intent intent = new Intent(<strong>DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN</strong>);
//<strong>声明myadmin组件</strong>
ComponentName myAdmin= new ComponentName(this,MyAdmin.class);
//相当于 在
//描述内容 显示在获得管理员权限窗口
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"哥们开启我可以一键锁屏,你的按钮就不会经常失灵");
//启动了DevicePolicyManager 的添加管理员行为
startActivity(intent);
最后 在DevicePolicyManager 中激活管理员权限
dpm.isAdminActive(myAdmin);
dpm.lockNow();锁屏api