Services stopping automatically from android OS 5.

2019-07-22 09:52发布

问题:

I am working on an application related to Voip & IM (chatting application). Till android OS 5.0 it is working good and no issues from client also. But from android OS 5.1.1 onwards we are facing issues.

Issues:

  • If the app is in foreground and if device goes to sleep mode it is working good.
  • If the app is in background and if device goes to sleep mode after 10 minutes all the services are stopping automatically.

Testing:

  • For testing I created one timer task to print logs for every 1 minute. So when I close the application (means if the app is in background)

  • Till android OS 5.0 logs are printing continuously.

  • From android OS 5.1.1 onwards after going to sleep mode, after 10 minutes logs are not printing. Timer task is stopping

Same problem I am facing for my application also.

After doing some R&D I came to know regarding doze mode and app optimization. Is it anyhow related to stop services automatically running in background.

If yes, can anyone please let me know how to overcome this issue.

回答1:

This is new thing which google done to prevent unwanted battery usage. For new versions the background services are restricted for better battery life. The android service consumes almost same as the application running in foreground. So even though app is not running its services may be running and using memory and battery.

If you have to use any kind of background process try to use Job Scheduler which may be something useful for you.

The Job Scheduler groups the task and execute the background task based on the constraints we provide in the implementation. This have a limitation it support on minimum API 21

To support on lower version you can use Firebase JobDispatcher which support from API 9.

I personally prefer Firebase JobDispatcher

This link help you to understand more about scheduler and its implementation.