Android: Sleep stages/levels on an Android device?

﹥>﹥吖頭↗ 提交于 2019-11-28 05:33:12
CommonsWare

From browsing the mailing lists, I'm aware that there exist a stage called "Deep Sleep".

There is not really a separate stage called "deep sleep". There is only "awake", "asleep", and "off".

Do execution for all apps halt when device reaches this state?

Execution of all processes ceases when the device goes to sleep or is powered off.

If so, besides user hitting the power button, what else could wake the device back up?

  • An alarm from AlarmManager
  • An incoming phone call
  • An incoming text message
  • If you have a socket open on wireless data (not WiFi), an incoming packet on that socket

Those are the big ones. There might be others.

I've noticed the following behaviour:

  1. You have your activity open and stop interacting with it
  2. After a few seconds (it depends on how the device is configured) the screen will go off.

    When the screen goes off, onSaveInstance and onPause are called.

  3. A few seconds later (usually ~15s) the device enters into sleep mode (is this the correct name?)

    When this happens, the following methods are invoked: onStop (calling isFinishing returns false), onRetainNonConfigurationInstance and onDestroy.

    So far so good. Now, the strange behaviour begins: just after the last onDestroy finishes, another activity is created: onCreate, onStart, onRestoreInstanceState, onResume and finally onPause are invoked.

    I find no reason for this strange behaviour. Why would another activity be created just to go straight to pause mode? This happens immediatly after onDestroy of the original activity!

This was tested on Galaxy S. I didn't test what happens after a few hours with no activity. I'm not sure if anything else will happen.

I hope this will help you.

user728732

A short addition to the commonsware's list. After looking for a way to run methods periodically while phone is asleep, I've found out that TimerTask functions during sleep mode.

TimerTask is, in my experience, easier to work with if all you want is to run methods from a service and not to start an activity.

Besides the "awake", "asleep", and "off" states that @CommonsWare mentioned, there is the distinction between whether the CPU is asleep, or just the screen is. For example, the official docs here describe it this way:

To avoid draining the battery, an Android device that is left idle quickly falls asleep. However, there are times when an application needs to wake up the screen or the CPU and keep it awake to complete some work. [emphasis added]

In the three-stage framework that CommonsWare described, a device whose screen is dark is probably not categorized as "asleep" unless the CPU is also stopped. But as the above paragraph implies, the screen-dark state can legitimately be referred to as "asleep." No doubt this is why people refer to "deep sleep" to clarify that they're talking about the CPU being asleep.

This doc page also mentions

When an Android device is left idle, it will first dim, then turn off the screen, and ultimately turn off the CPU. This prevents the device's battery from quickly getting drained.

So if you want to be comprehensive, you could add "dim" to the list of "sleep stages/levels":

  1. awake
  2. dim
  3. screen off
  4. CPU off (true "sleep" or "deep sleep")
  5. power off

Apparently the transition from 2 to 3 to 4 is pretty fast when the idle timeout occurs. But there are other times when the screen can be off without a transition to deep sleep; e.g. when playing audio (at least in certain apps).

I wish I could tell you how to predict when the device will transition from screen off to CPU off -- e.g. how long the timeout is -- but I haven't found that information. What I have found is FLAG_KEEP_SCREEN_ON and WAKE_LOCK to prevent one or the other from happening.

P.S. If you want to be exhaustive, you could count daydream in your list of "sleep stages":

Daydream is a new [as of Android 4.2] interactive screensaver mode for Android devices. It activates automatically when the device is inserted into a dock or when the device is left idle while plugged in to a charger (instead of turning the screen off).

From the point of view of the previously-running app, it sounds like daydream behaves like switching to a different app. So it's not really a matter of the device sleeping, though your activity does get stopped, I would assume.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!