作者:手机用户2502853443_910 | 来源:互联网 | 2023-10-16 13:11
从Android2.2开始,加入了一个新的锁屏API位于android.app.admin.DevicePolicyManager包,DevicePolicyManager类的lockNow方法可以
从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);
- }
- }
这里Android123提示大家传统的方法加入权限,使用下面代码可以锁住键盘,但屏幕不行
- KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
- KeyguardLock kl= km.newKeyguardLock(KEYGUARD_SERVICE);
- kl.reenableKeyguard();