1,创建类DeviceAdminReceiver的子类
public class DeviceAdminSample extends DeviceAdminReceiver {
}
2,在清单文件中配置广播接收者
<receiver
android:name="com.itheima62.lockscreen.DeviceAdminSample"
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>
3,配置字符串相关信息
<string name="activity_sample_device_admin">设备管理员</string>
<string name="sample_device_admin">管理员</string>
<string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
4,在res目录下创建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>
5,在代码中创建设备管理器和组件
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
ComponentName who = new ComponentName(this, DeviceAdminSample.class);
6,写功能
//获取设备管理器
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
//设置密码
dpm.resetPassword("123", 0);
//一键锁屏
dpm.lockNow();
dpm.lockNow();一键锁屏
public class DeviceAdminSample extends DeviceAdminReceiver {
}
2,在清单文件中配置广播接收者
<receiver
android:name="com.itheima62.lockscreen.DeviceAdminSample"
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>
3,配置字符串相关信息
<string name="activity_sample_device_admin">设备管理员</string>
<string name="sample_device_admin">管理员</string>
<string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
4,在res目录下创建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>
5,在代码中创建设备管理器和组件
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
ComponentName who = new ComponentName(this, DeviceAdminSample.class);
6,写功能
//获取设备管理器
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
//设置密码
dpm.resetPassword("123", 0);
//一键锁屏
dpm.lockNow();
dpm.lockNow();一键锁屏