2

I am running service background to execute some tasks with some interval using AlaramManager (setExactAndAllowWhileIdle). If its connected the power charger its running. But if its unplugged device power after some i am loosing the network to my application.

Alaram is waking up the service even device is idle.But my app don't have network.

As per https://developer.android.com/training/monitoring-device-state/doze-standby.html# its restricting the network when phone is idle.I tested the same with applying phone idle ($ adb shell dumpsys battery unplug)

But is there any possible to get network access to my application even phone is idle mode.

Application is not of play store. Its really appropriated for suggestion.

Srikanth
  • 584
  • 1
  • 6
  • 21

1 Answers1

1

I've used PowerManager.isDeviceIdleMode() in your running service to detect whether you're in Doze and if so just invoke setAlarmClock() method with 250 millis (for example) and random broadcast just to wake the device up, it'll completely wake up from doze and thus will get network access as usual. THe down side is that you lose the battery saving that comes with doze.. :D Please mark this as useful should it actually be usefull in your case. Once your service (set up with setExactAndAllowWhileIdle()) you can go :

private boolean isDeviceInDozeMode() {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
return pm.isDeviceIdleMode();

So now you know that you're in DOZE or not.

Then you can just set up AlarmClock and set it with setAlarmClock(AlarmClockInfo a, PendingIntent p)

And you're out of Doze with normal network access.

NOTE: Please remember that using setExactAndAllowWhileIdle doesn't mean that your alarm will go off at specified time, it will go off sooner than normal set() method, but not exact (as the method name would suggest). Generally my way isn't perfect in terms of scheduling, but it does exit DOZE programmatically once it's detected on device.

Let me know should you need more update.

  • how can we use PowerManager.isDeviceIdleMode() for service to detect the phone doze mode and where to set alaram. If you don't can you send spinet code – Srikanth Apr 27 '17 at 15:22
  • If my service executed after that i schedule next run lets say 60 min (setExactAndAllowWhileIdle) by the time if device is doze can get network access. How deal this scenario. – Srikanth Apr 27 '17 at 16:19
  • added more info in my answer, please review and let me know should you need more info. – Krzysztof Kubicki Apr 28 '17 at 08:05
  • I have some couple of scenarios – Srikanth Apr 28 '17 at 13:03
  • 1. My AlarmManager will start the Intentserverice and then service finish task and alarm schedule for next time in same meaner. 2. In 1st case while running service i may loose network and 2nd case after finish the service there is gap 1 hour i may loos the network in gap aswell. 3. If its a during the service where to call isDeviceInDozeMode() to check all the time because we never no when we loos the network 4. If next time start alarm to wakeup server if dont have network how check. 5.setAlarmClock is to wakup the alarm or servier and where to use. sorry for bad English. – Srikanth Apr 28 '17 at 13:10
  • For DeviceIdleMode used BroadcastReceiver to detect, but how to set setAlarmClock and should i use any pending intent – Srikanth Apr 28 '17 at 20:09
  • AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); am.setAlarmClock(new AlarmManager.AlarmClockInfo(250, sender), sender); – Srikanth Apr 28 '17 at 21:08
  • I used the above to setAlaramClock in BroadcastReceiver of DeviceIdleMode .. But no luck... – Srikanth Apr 28 '17 at 21:09