简体   繁体   中英

How can I make my app run as a background process

I want to make my app run in the background like a process that runs always.

I need it because I want to get locations update for GPS every 2 minutes (longitude, latitude) and to use the information in a method.

For that I need for the app to be running when the phone is asleep or not in the UI of the app in other words I need the app will run always.

I'm sure that there is a way to make it , thanks anyway for any answers :)

This was just the first google search result I found: http://www.vogella.com/articles/AndroidServices/article.html

The answer here is to use a service , if this tutorial is lacking there are 6.4 billion others.

We have something like this, but it is made up of several parts.

Firstly you will want your code to run (and be registered in the manifest) as a Service

You will probably also want to request android.permission.RECEIVE_BOOT_COMPLETED so that you can write and register a BroadcastReceiver that gets notified by android.intent.action.BOOT_COMPLETED action and its onReceive method kicks off the service.

In our case we also have a front-end activity which also pokes the service to make sure it is running, but it's been a while snce I checked to see if this was still required.

Our service is nearly empty and onCreate immediately calls a custom Handler which then manages the 'ticks' which wakes the Handler and fires a Runnable if there is work to do, but this is where my code diverges from yours. In our case we only attempt to update the GPS location when the service 'ticks' (usually every minute) and there is work to do. It usually only performs a couple of dozen operations per client per day so I can't really advise on how it will impact battery usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM