当前位置:   article > 正文

PowerManager管理亮灭屏操作_powermanager.acquire_causes_wakeup | powermanager.

powermanager.acquire_causes_wakeup | powermanager.screen_bright_wake_loc
  1. /* 注册监听亮灭屏广播,当灭屏时,点亮屏幕 */
  2. BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. Log.d(TAG, "action = " + intent.getAction());
  6. if (Intent.ACTION_SCREEN_ON.equalsIgnoreCase(intent.getAction())) {
  7. Log.d(TAG, "screen on.");
  8. mScreenOnTxt.setText("screen on");
  9. } else if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
  10. Log.d(TAG, "screen off");
  11. mScreenOnTxt.setText("screen off");
  12. wakeUpScreen();
  13. }
  14. }
  15. };
  16. private void registerScreenOnReceiver() {
  17. IntentFilter filter = new IntentFilter();
  18. filter.addAction(Intent.ACTION_SCREEN_ON);
  19. filter.addAction(Intent.ACTION_SCREEN_OFF);
  20. registerReceiver(mBroadcastReceiver, filter);
  21. }
  22. private void wakeUpScreen() {
  23. PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  24. if (mWakeLock == null) {
  25. mWakeLock = powerManager.newWakeLock(
  26. PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,
  27. "MainActivity:wakeup");
  28. }
  29. /* 如果屏幕亮屏状态,就不再点亮 */
  30. if (powerManager.isScreenOn()) {
  31. return;
  32. }
  33. if (mWakeLock.isHeld()) {
  34. mWakeLock.release(); //需要释放锁,调用acquire点亮屏幕
  35. }
  36. mWakeLock.acquire();
  37. }
  38. @Override
  39. protected void onStop() {
  40. super.onStop();
  41. }
  42. @Override
  43. protected void onDestroy() {
  44. super.onDestroy();
  45. unregisterReceiver(mBroadcastReceiver);
  46. if (mWakeLock != null && mWakeLock.isHeld()) {
  47. mWakeLock.release();
  48. }
  49. }

wakelock几种亮屏类型定义:

类型 cpu屏幕键盘
PARTIAL_WAKE_LOCKonoffoff
SCREEN_DIM_WAKE_LOCKondimoff
SCREEN_BRIGHT_WAKE_LOCKonBrighoff
FULL_WAKE_LOCKonBrighBrigh

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/301729
推荐阅读
相关标签
  

闽ICP备14008679号