0

I am making a real-time location tracker android app. Yes there are lot of duplicate questions but here is my scenario. Device's latitudes and longitude are changing continuously, So I have to send the data to at server almost every 2 seconds.

http://myserver.com/tracker.php?lat=10&long=5&guid=123456

On the other side another device will be fetching the location continuously every 2 second

http://myserver.com/getlocation.php?guid=123456

I am new in android development but googled about GCM, Bacground services and asyncTask. Battery drain is not a issue for now. and I concerned about background services too. What will be the best approach to send and fetch the data every 2 seconds ? using AsyncTask or something else ?

Abhigyan
  • 641
  • 10
  • 25

2 Answers2

0

It depends, if your server knows which devices should fetch the data, you can best use Google Cloud Messaging, though there might be a small delay which you will not like. If you want to poll for the data in your devices than use a separate thread (or AsyncTask which is just a thread with some wrapper code).

Additionally you can also use long Polling see this question for some insight into that. But that is on the server side, and still requires a thread in your app. Inside a separate thread you can use runOnUiThread to update the user interface, or use the success and progress functions of the AsyncTask

Community
  • 1
  • 1
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
0

You can use Async Task which is the Best approach which is followed in the community.You can use handler inside your code to call the Async Task after every 2 seconds

AsyncTask should ideally be used for operations that take few seconds. Some tasks keep the thread running for long time so in that case it is recommended to use java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

Ashish Shukla
  • 1,027
  • 16
  • 36