Service is stopped after a few minuets of inactivity

蹲街弑〆低调 提交于 2019-12-11 18:03:24

问题


I am writing an android app using the GPS location provider in a Service that is started when the main Activity is created (unless the service is already running). I was under the impression that the service would continue to run with the unlikely exception that the task manager could killed it if there was a memory shortage. For some reason the service is restarted if there is a period of very low activity regardless of how much memory is available. When the user is outside and the GPS is able to resolve a position, the service will get a position every 30 seconds and turn off the gps in between (to save battery). In this case the service continues to run without any problems but when a user moves inside and a gps position can't be found within five seconds, then the service turns off the gps and will only reactivate the GPS every 200 seconds to check if the user has moved back outdoors. In this case the service will sometimes be stopped and won't be restarted until the app is reopened and the activity restarts the service. I created a separate service to make sure the process was uninterrupted. Does anyone know why this might be happening? Thanks so much for your help! -Dom


回答1:


I would have a look at Mark Murphy's example that deals with Services and Wakelocks:

WakefulIntentService

If you can't figure it out from his code example, then you want to get his Advanced Android book where the problem is explained in detail.

The Busy Coder's Guide to Advanced Android Development

These are some of the best books on Android development and well worth the minimal cost.



来源:https://stackoverflow.com/questions/5766632/service-is-stopped-after-a-few-minuets-of-inactivity

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