jarly
jarly

Reputation: 231

Does device enter doze mode while foreground service running

My app needs to run in background for long time and periodically send data to server. I tried AlarmManager but it is restricted in doze mode. I next thought is the foreground service.

Does the device enter doze mode while a foreground service is running?

If the device enters doze mode, is internet connection restricted while foreground service is running?

Upvotes: 1

Views: 1647

Answers (1)

from56
from56

Reputation: 4127

That works for me pretty well, tested several hours of continuous operation with some emulators and Android 6.0 and 7.1 real devices, network connection never lost :

In the Main Activity onPause I acquire a Partial WakeLock and I start a Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows a Notification.

On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf.

And also that avoids Activity to be destroyed on Back Key press :

 @Override
    public void onBackPressed() {
         moveTaskToBack(true);
    }

Of course I provide a menu option to stop the background porcessing and finish the Activity

My code that periodically ask an internet server is placed in the Activity, it uses a timer.

Hope it will help

Upvotes: 1

Related Questions