Bilal Soomro
Bilal Soomro

Reputation: 671

Should I use a background service or will AsyncTask suffice for this task?

This is my first android app and so I am a little confused on what is the best method to accomplish this.

My app is going to get the GPS coordinated using the location services and save them in an online MySQL database of a PHP based website. Now I already have the code to retrieve GPS coordinates working and I am working on passing it to the online database. I am thinking about using AsyncTask to connect to the online database by making a POST request. Since the app will be constantly giving new coordinates, I was planning on executing the AsynTask in the onLocationChanged() method of my activity so whenever the coordinates are updates, the new updated coordinates are also updated in my online database.

Now my question is that is it okay to execute my AsyncTask in the onLocationChanged() method or is there a better way? Should I create a service to do the saving and start the service inside onLocationChanced method which will run the service everytime new coordinates are updated.

Another question I had was if the user opts out of the app, i am assuming my app will stop sending its coordinates to my online database if I implement it using AsyncTask in my activity. Should I create a service if I want it to keep sending the coordinates unless the user wants to stop the app and he will open it and maybe press a button to kill that service.

Any help would be much appreciated. Thanks

Upvotes: 1

Views: 90

Answers (1)

elmorabea
elmorabea

Reputation: 3263

Go with the background service if you want your app to keep sending the location even if the user has navigated away from your application, if that's not necessary then an async task should be sufficient.

Note: if your update interval is short, say multiple times per minute for example .. consider batching multiple updates then sending them at once to save power and network bandwidth

Upvotes: 2

Related Questions