Android service with always on GPS

2019-08-18 17:11发布

I've created an android app that is so far working quite well. It contains a local service which is responsible for getting the users location every couple of minutes and relaying that via UDP to a collection server which is in turned displayed on a web application map.

The app needs to be constantly running unless the user explicitly signs out of the application. Currently, it seems the OS is shutting down the service and restarting it as needed. I can see this because normally it would report its location consistently every 2 minutes. Instead, there are gaps in these intervals.

So I'm looking for some advice on how I can write a service which (at the battery's expense unfortunately) can stay running always and pool the location service just enough to send accurate fixes at a 2 minute interval.

I have a well working code base so far, so if demonstrating where I currently am with code would help, don't hesitate to ask.

Thanks in advance to those that lend a hand.

1条回答
Bombasti
2楼-- · 2019-08-18 17:53

Your best bet is to use the AlarmManager to wake up every two minutes, do some processing and quickly go back to sleep.

If you think you're hardcore, you could even use a wake lock, but that will keep the phone's CPU turned on persistently without mercy. You don't want that.

查看更多
登录 后发表回答