4

Android M has a new feature called App Standby where an app is put into an idle state when it's not being utilized (see docs) and, among other things, its network access is disabled.

I can't seem to figure out a way to determine when the app goes into and out of this state (via a broadcast intent or something of the like) and I really need to as my app relies on having network periodically to check the status of a server.

Can someone help me figure out how to determine when my app goes into and out of idle state?

I've been digging through the M preview 2 source and down through the calls of $ adb shell am set-inactive it appears that there's a AppIdleStateChangeListener but it appears to only be used internally to Android and isn't exposed to us lowly developers who want to know when our apps can use the internet :-(

Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
  • "isn't exposed to us lowly developers who want to know when our apps can use the internet" -- if your code is running, you're not in app standby or Doze mode. IOW, I would not expect most apps to have to do anything special here, other than have the `AlarmManager` or `JobScheduler` work established, or be set up for other broadcasts that you normally listen to. Am I missing something? – CommonsWare Aug 13 '15 at 21:11
  • @CommonsWare "if your code is running, you're not in app standby or Doze mode" -- false. Test it yourself: have an app set an alarm to send a network request, set the alarm and then put the app into idle mode. When it goes off, the network request will fail indicating that the app is still in standby. The docs state that the only time apps won't go into/come out of this mode is based on the 4 criteria listed under: http://developer.android.com/preview/behavior-changes.html#behavior-power – Reuben Tanner Aug 13 '15 at 21:43
  • 1
    "the network request will fail indicating that the app is still in standby" -- or that there is no network connection for other reasons. Your app still needs to handle that. – CommonsWare Aug 13 '15 at 21:47
  • check this: [http://stackoverflow.com/questions/31337904/android-m-doze-mode-and-app-standby][1] [1]: http://stackoverflow.com/questions/31337904/android-m-doze-mode-and-app-standby – Joanmi Bardera Aug 13 '15 at 22:39
  • @CommonsWare, you bring up a good point...I really should be handling this case irrespective of App Standby. Thank you +1 – Reuben Tanner Aug 14 '15 at 14:27
  • @JoanmiBardera the solutions that the OP found handle device idle which is separate from app standby. – Reuben Tanner Aug 14 '15 at 14:28

1 Answers1

0

So far, it appears that when ACTION_POWER_CONNECTED is broadcast, all apps come out of standby...this is a potential workaround if Google doesn't expose something for us before release.

Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
  • 1
    Listen to this action broadcast android.os.action.DEVICE_IDLE_MODE_CHANGED once you get this action changed event check the device idle state using below API. PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); boolean isDeviceIdle = pm.isDeviceIdleMode(); – Rupali Aug 17 '15 at 11:30
  • That is a device wide idle, not an app specific idle. Apps can go into and out of idle without the device being idle. – Reuben Tanner Aug 17 '15 at 11:33
  • https://code.google.com/p/android-developer-preview/issues/detail?can=2&start=0&num=100&q=Type%3DDefect&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=-id&id=3164 – Reuben Tanner Sep 10 '15 at 16:28