Services stopping automatically from android OS 5.1.1

狂风中的少年 提交于 2019-12-11 05:27:29

问题


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.



来源:https://stackoverflow.com/questions/44897589/services-stopping-automatically-from-android-os-5-1-1

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