从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); } }
KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); KeyguardLock kl= km.newKeyguardLock(KEYGUARD_SERVICE); kl.reenableKeyguard();