Android 锁屏 DevicePolicyManager

   /**
     * Make the device lock immediately, as if the lock screen timeout has
     * expired at the point of this call.
     * 
     * <p>The calling device admin must have requested
     * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
     * this method; if it has not, a security exception will be thrown.
     */
    public void lockNow() {
        if (mService != null) {
            try {
                mService.lockNow();
            } catch (RemoteException e) {
                Log.w(TAG, "Failed talking with device policy service", e);
            }
        }
    }

这段代码告诉我们 如果你想锁住屏幕 你必须获得 USES_POLICY_FORCE_LOCK 权限,那么在我们锁屏幕之前就必须请求权限

代码如下:
ComponentName mComponentName = new ComponentName(TestDeviceScreenLock.this, AdminReceiver.class);

Intent intent = new Intent(DevicePolicyManager.ACTIntent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mComponentName);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Additional text explaining why this needs to be added.");
                startActivityForResult(intent, 1);


TestDeviceScreenLock.this 就是Context的继承类
AdminReceiver.class是一个DeviceAdminReceiver 此类继承 BroadcastReceiver

但是锁屏之后 不容易解屏 必须得按BACK键  不知是为什么?望高手解答...


你可能感兴趣的:(android)