问题
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