从Android 2.2开始,加入了一个新的锁屏API位于android.app.admin.DevicePolicyManager包,DevicePolicyManager类的lockNow方法可以锁住屏幕,查看Android源代码发现其实是从IDevicePolicyManager实现的,整个AIDL接口调用代码为:
-
- private final IDevicePolicyManager mService;
-
- mService = IDevicePolicyManager.Stub.asInterface(
- ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
-
- if (mService != null) {
- try {
- mService.lockNow();
- } catch (RemoteException e) {
- Log.w(TAG, "Failed talking with device policy service", e);
- }
- }
复制代码 这里提示大家传统的方法加入<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>权限,使用下面代码可以锁住键盘,但屏幕不行
- KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
- KeyguardLock kl= km.newKeyguardLock(KEYGUARD_SERVICE);
- kl.reenableKeyguard();
复制代码
留言列表