权限:
代码: DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName adminComponent = new ComponentName(this, WDeviceAdminReceiver.class); if (devicePolicyManager.isAdminActive(adminComponent)) { devicePolicyManager.lockNow(); } else { // 如果设备管理器未激活,请提示用户激活设备管理器 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "请激活设备管理器以使用锁屏功能"); startActivity(intent); }
广播:
public class WDeviceAdminReceiver extends DeviceAdminReceiver { //空的就行 }
res下创建XML文件夹创建device_admin::