0

I'm not 100% with the Android life cycle at this point. I want to run a process/ thread, I'm not sure of the correct term here, all the time the handset is on and for it to run without the user activating the app. For part of a game I am working on I want to transmit the users rough location back to the server at regular intervals and also get some data from the server. What is the correct way to go about this in android?

James MV
  • 8,569
  • 17
  • 65
  • 96

2 Answers2

3

You have to look at Android Service. here you can find a good introduction about this topic.

Also for detecting when the user has plugged in/out the headset you will have to use an BroadcastReceiver. It is already a topic about this here.

Community
  • 1
  • 1
niculare
  • 3,629
  • 1
  • 25
  • 39
1

If this background process is doing more work than just upload location, @niculare answer is correct indeed.

But, if the only thing that this background process will do is this upload, there're more battery efficient ways of doing it. You can create a BroadcastReceiver to receive location updates from the System and do the upload. If you need some small processing before the upload you can even start a Service from the BroadcastReceiver, then the service will process the data, upload it finish.

Also for both situations you're likely to need a BroadcastReceiver to receive a OnBoot broadcast, to know that the mobile just started, and register those stuff.

Small update you Register with this method for location updates using this listener

Budius
  • 39,391
  • 16
  • 102
  • 144