As the answer of @BiGGZ says, OnLocationChanged() is best way to do that. Here I'm including sample code, Which runs in Service to track location in background.
public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
private int i=0;
private GoogleApiClient mGoogleApiClient;
private DatabaseHelper myDb;
private LocationRequest mLocationRequest;
private int uniqueInt=0;
private PowerManager.WakeLock wakeLock;
private String templat, templong;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
myDb =new DatabaseHelper(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mGoogleApiClient.connect();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
mGoogleApiClient.disconnect();
wakeLock.release();
super.onDestroy();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,"GoogleApiClient connection has been suspend",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"GoogleApiClient connection has failed",Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude(), lon = location.getLongitude();
// Toast.makeText(this,lat+"--->"+lon,Toast.LENGTH_SHORT).show();
//Do your task here!!
}
}
And I've taken frequency of tracking location 1 second, If you want to save battery, Change it to 5 or 10 seconds.
EDIT:
For calculate speed of your device, use following method for calculate difference between your 2 tracked coordinates(in meter) and then divide this by your tracking frequency(ex. divide by 5 if you are tracking location every 5 sec.). Which will give you speed in m/s.
public double distance(String lat1, String lon1, String lat2, String lon2){
double earthRadius = 6378.137;
double dLat = Double.parseDouble(lat2)*Math.PI/180 - Double.parseDouble(lat1)*Math.PI/180;
double dLon = Double.parseDouble(lon2)*Math.PI/180 - Double.parseDouble(lon1)*Math.PI/180;
double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Double.parseDouble(lat1)*Math.PI/180) * Math.cos(Double.parseDouble(lat2)*Math.PI/180)*
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
double d = earthRadius*c;
return d*1000; //distance in meter.
}
Hope this helps :)