从Android 2.2开始,加入了一个新的锁屏API位于android.app.admin.DevicePolicyManager包,DevicePolicyManager类的lockNow方法可以锁住屏幕,查看Android源代码发现其实是从IDevicePolicyManager实现的,整个AIDL接口调用代码为:

  1.       private final IDevicePolicyManager mService;
  2.    mService = IDevicePolicyManager.Stub.asInterface(
  3.                 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
  4.   if (mService != null) {
  5.             try {
  6.                 mService.lockNow();
  7.             } catch (RemoteException e) {
  8.                 Log.w(TAG, "Failed talking with device policy service", e);
  9.             }
  10.         }
复制代码

这里提示大家传统的方法加入<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>权限,使用下面代码可以锁住键盘,但屏幕不行

  1.    KeyguardManager km =    (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); 
  2.    KeyguardLock kl= km.newKeyguardLock(KEYGUARD_SERVICE);  
  3.    kl.reenableKeyguard();
复制代码
arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()