1. /**
  2.  
    * 唤醒手机屏幕并解锁
  3.  
    */
  4.  
    public void wakeUpAndUnlock() {
  5.  
    // 获取电源管理器对象
  6.  
    PowerManager pm = (PowerManager) getApplicationContext()
  7.  
    .getSystemService(Context.POWER_SERVICE);
  8.  
    boolean screenOn = pm.isScreenOn();
  9.  
    Log.d(“WakeScreen0”,“screenOn: ” + screenOn);
  10.  
    if (!screenOn) {
  11.  
    // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
  12.  
    @SuppressLint(“InvalidWakeLockTag”) PowerManager.WakeLock wl = pm.newWakeLock(
  13.  
    PowerManager.ACQUIRE_CAUSES_WAKEUP |
  14.  
    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, “bright”);
  15.  
    wl.acquire(10000); // 点亮屏幕
  16.  
    wl.release(); // 释放
  17.  
    }
  18.  
    // 屏幕解锁
  19.  
    KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext()
  20.  
    .getSystemService(KEYGUARD_SERVICE);
  21.  
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(“unLock”);
  22.  
    // 屏幕锁定
  23.  
    // keyguardLock.reenableKeyguard();
  24.  
    keyguardLock.disableKeyguard(); // 解锁
  25.  
    unLockScreen();
  26.  
    }
  27.  
     
  28.  
    private void unLockScreen(){
  29.  
    final Window win = getWindow();
  30.  
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
  31.  
    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  32.  
     
  33.  
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  34.  
    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
  35.  
    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
  36.  
    }

 

  1.  
    /**
  2.  
    * 唤醒手机屏幕并解锁
  3.  
    */
  4.  
    public void wakeUpAndUnlock(Activity activity) {
  5.  
     
  6.  
    Window win = activity.getWindow();
  7.  
    win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
  8.  
    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
  9.  
    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
  10.  
     
  11.  
    PowerManager pm = (PowerManager) activity.getSystemService(POWER_SERVICE);
  12.  
    @SuppressLint(“InvalidWakeLockTag”)
  13.  
    PowerManager.WakeLock wakelock = pm.newWakeLock(
  14.  
    PowerManager.FULL_WAKE_LOCK
  15.  
    | PowerManager.ACQUIRE_CAUSES_WAKEUP, “xx”);
  16.  
    wakelock.acquire();
  17.  
    wakelock.release();
  18.  
     
  19.  
    KeyguardManager keyguardManager = (KeyguardManager) activity.getApplicationContext()
  20.  
    .getSystemService(KEYGUARD_SERVICE);
  21.  
     
  22.  
    if (mHomeActivity == null) return;
  23.  
    keyguardManager.requestDismissKeyguard(mHomeActivity, new KeyguardManager.KeyguardDismissCallback() {
  24.  
    @Override
  25.  
    public void onDismissError() {
  26.  
    super.onDismissError();
  27.  
    Log.d(“xxx–>”, “1 onDismissError”);
  28.  
    }
  29.  
     
  30.  
    @Override
  31.  
    public void onDismissSucceeded() {
  32.  
    super.onDismissSucceeded();
  33.  
    Log.d(“xxx–>”, “1 onDismissSucceeded”);
  34.  
    }
  35.  
     
  36.  
    @Override
  37.  
    public void onDismissCancelled() {
  38.  
    super.onDismissCancelled();
  39.  
    Log.d(“xxx–>”, “1 onDismissCancelled”);
  40.  
    }
  41.  
    });
  42.  
     
  43.  
    if (mRemoteActivity == null) return;
  44.  
    keyguardManager.requestDismissKeyguard(mRemoteActivity, new KeyguardManager.KeyguardDismissCallback() {
  45.  
    @Override
  46.  
    public void onDismissError() {
  47.  
    super.onDismissError();
  48.  
    Log.d(“xxx–>”, “2 onDismissError”);
  49.  
    }
  50.  
     
  51.  
    @Override
  52.  
    public void onDismissSucceeded() {
  53.  
    super.onDismissSucceeded();
  54.  
    Log.d(“xxx–>”, “2 onDismissSucceeded”);
  55.  
    }
  56.  
     
  57.  
    @Override
  58.  
    public void onDismissCancelled() {
  59.  
    super.onDismissCancelled();
  60.  
    Log.d(“xxx–>”, “2 onDismissCancelled”);
  61.  
    }
  62.  
    });
  63.  
     
  64.  
    }

 

转载: 

https://blog.csdn.net/xinpengfei521/article/details/73065769/

http://www.voidcn.com/article/p-qvvwwkxf-hy.html