You this following code to track changes in location events
DeviceLocation.java
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class DeviceLocation extends Service {
private GPSTracker gpsTracker;
private Handler handler = new Handler();
private Timer timer = new Timer();
private Distance pastDistance = new Distance();
private Distance currentDistance = new Distance();
public static double DISTANCE;
boolean flag = true;
private double totalDistance;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.v("Location1", "********* " + Thread.currentThread().getId());
gpsTracker = new GPSTracker(this);
// below code is to track change in past and present location events
// TimerTask timerTask = new TimerTask() {
//
// @Override
// public void run() {
// Log.v("Location2", "********* " + Thread.currentThread().getId());
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// Log.v("Location3", "********* " + Thread.currentThread().getId());
// if (flag) {
// pastDistance.setLatitude(gpsTracker.getLocation().getLatitude());
// pastDistance.setLongitude(gpsTracker.getLocation().getLongitude());
// flag = false;
// } else {
// currentDistance.setLatitude(gpsTracker.getLocation().getLatitude());
// currentDistance.setLongitude(gpsTracker.getLocation().getLongitude());
// flag = comapre_LatitudeLongitude();
// }
// }
// });
// }
// };
// timer.schedule(timerTask, 0, 5000);
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Log.v("Location2", "********* " + Thread.currentThread().getId());
handler.post(new Runnable() {
@Override
public void run() {
// send to server or device you need to send via GCM
SendMessage(String.valueOf(gpsTracker.getLocation().getLatitude())
String.valueOf(gpsTracker.getLocation().getLongitude()));
}
});
}
};
timer.schedule(timerTask, 0, 5000);
return START_STICKY;
}
public void Track(String lat, String lng) {
Sender sender = new Sender("AIzaSyAlXGdj3TGPAnKBiuhn4yxXgLKxwmHJnMY");
String GcmId = new TrackPref(this).getTrackId();
Message message = new Message.Builder()
.collapseKey("and_app")
.delayWhileIdle(true)
.addData("action", "location")
.addData("Lat", lat)
.addData("Long", lng)
.timeToLive(600)
.build();
Result result = null;
try {
result = sender.send(message, GcmId, 2);
} catch (IOException e) {
e.printStackTrace();
}
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
Log.d("abc", result.toString());
} else {
String error = result.getErrorCodeName();
}
}
private void SendMessage(final String lat, final String lng) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
Track(lat, lng);
return null;
}
@Override
protected void onPostExecute(String msg) {
}
}.execute();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("--------------------------------onDestroy -stop service ");
timer.cancel();
DISTANCE = totalDistance ;
}
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
public boolean comapre_LatitudeLongitude() {
Log.v("Location4", "********* " + Thread.currentThread().getId());
if (pastDistance.getLatitude() == currentDistance.getLatitude() && pastDistance.getLongitude() == currentDistance.getLongitude()) {
return false;
} else {
final double distance = distance(pastDistance.getLatitude(), pastDistance.getLongitude(), currentDistance.getLatitude(), currentDistance.getLongitude());
System.out.println("Distance in mile :" + distance);
handler.post(new Runnable() {
@Override
public void run() {
float kilometer = 1.609344f;
Log.v("Location5", "********* " + Thread.currentThread().getId());
totalDistance = totalDistance + distance * kilometer;
DISTANCE = totalDistance;
}
});
return true;
}
}
}
For detailed information follow the code from below library.
https://github.com/raviteja06/TrackMyClient
this was done while back, make sure to update libraries to latest.