How to find whether phone is in sleep/idle mode for Android

后端 未结 4 1806
离开以前
离开以前 2020-12-05 16:39

How to find whether phone is in sleep/idle mode for Android?

My problem is I am able to wake up the phone from sleepmode by using alarm manager

But when the

相关标签:
4条回答
  • 2020-12-05 17:02

    I'm not sure thats what you want, but you can use Intent.ACTION_SCREEN_OFF to determine. see here an example.

    0 讨论(0)
  • 2020-12-05 17:04

    You can check if the screen is turned off by calling isScreenOn method.

    Note:This only can be used for 2.1 and above..

    0 讨论(0)
  • 2020-12-05 17:14

    isScreenOn method returns only if the screen is on or off. It does not indicate the sleepy mode, because Android sleep mode is not same as Android screen off mode. In Android, sleep mode might come within milliseconds (depending on wakelocks) after screen off (idle mode) - but that's not guaranteed (referring to this post).

    I think you can try using SystemClock.uptimeMillis(),SystemClock.elapsedRealtime() comparison.

    0 讨论(0)
  • 2020-12-05 17:24

    Check out isInteractive()

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
      return pm.isInteractive();
    } else {
      return pm.isScreenOn();
    }
    
    0 讨论(0)
提交回复
热议问题