Skip to content

Instantly share code, notes, and snippets.

@umesh0492
Last active May 24, 2016 07:02
Show Gist options
  • Save umesh0492/c9b07f69762ceb1eeebc255a92aaf556 to your computer and use it in GitHub Desktop.
Save umesh0492/c9b07f69762ceb1eeebc255a92aaf556 to your computer and use it in GitHub Desktop.
Track Location Update (Tested code)
//You need to start the service
startService (new Intent (this, LocationListenerService.class));
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
/**
* Created by umesh0492 on 15/03/16.
*/
public class LocationListenerService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
public void onCreate () {
super.onCreate ();
checkPlayServices ();
buildGoogleApiClient ();
createLocationRequest ();
}
@Override
public int onStartCommand (Intent intent, int flags, int startId) {
// Resuming the periodic location updates
if (mGoogleApiClient.isConnected ()) {
startLocationUpdates ();
}
return START_STICKY;
}
/**
* Starting the location updates
*/
protected void startLocationUpdates () {
if (mGoogleApiClient.isConnected ()) {
LocationServices.FusedLocationApi.requestLocationUpdates (mGoogleApiClient, mLocationRequest,
new com.google.android.gms.location.LocationListener () {
@Override
public void onLocationChanged (Location location) {
if (null != location) {
// Utils.logError ("lng", location.getLongitude () + "");
// Utils.logError ("lat", location.getLatitude () + "");
}
else {
try {
double lat = LocationServices
.FusedLocationApi
.getLastLocation (mGoogleApiClient).getLatitude ();
double lng = LocationServices
.FusedLocationApi
.getLastLocation (mGoogleApiClient).getLongitude ();
//Utils.logError ("lng", lng + "");
//Utils.logError ("lat", lat + "");
} catch (NullPointerException ignored) {
}
}
}
});
}
}
@Override
public void onDestroy () {
super.onDestroy ();
if (mGoogleApiClient.isConnected ()) {
mGoogleApiClient.disconnect ();
}
}
/**
* Return the communication channel to the service. May return null if
* clients can not bind to the service. The returned
* {@link IBinder} is usually for a complex interface
* that has been <a href="{@docRoot}guide/components/aidl.html">described using
* aidl</a>.
* <p/>
* <p><em>Note that unlike other application components, calls on to the
* IBinder interface returned here may not happen on the main thread
* of the process</em>. More information about the main thread can be found in
* <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and
* Threads</a>.</p>
*
* @param intent The Intent that was used to bind to this service,
* as given to {@link Context#bindService
* Context.bindService}. Note that any extras that were included with
* the Intent at that point will <em>not</em> be seen here.
* @return Return an IBinder through which clients can call on to the
* service.
*/
@Nullable
@Override
public IBinder onBind (Intent intent) {
return null;
}
@Override
public void onTaskRemoved (Intent rootIntent) {
super.onTaskRemoved (rootIntent);
}
public void checkPlayServices () {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance ();
int resultCode = apiAvailability.isGooglePlayServicesAvailable (MainApplication.context);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError (resultCode)) {
// apiAvailability.getErrorDialog(this, resultCode, 9000).show();
}
else {
//Log.i(TAG, "This device is not supported.");
stopSelf ();
}
}
}
/**
* Creating google api client object
*/
protected synchronized void buildGoogleApiClient () {
mGoogleApiClient = new GoogleApiClient.Builder (this)
.addConnectionCallbacks (this)
.addOnConnectionFailedListener (this)
.addApi (LocationServices.API).build ();
}
/**
* Creating location request object
*/
protected void createLocationRequest () {
mGoogleApiClient.connect ();
mLocationRequest = new LocationRequest ();
int UPDATE_INTERVAL = 1800000;
mLocationRequest.setInterval (UPDATE_INTERVAL);
int FASTEST_INTERVAL = 300000;
mLocationRequest.setFastestInterval (FASTEST_INTERVAL);
mLocationRequest.setPriority (LocationRequest.PRIORITY_HIGH_ACCURACY);
int DISPLACEMENT = 50;
mLocationRequest.setSmallestDisplacement (DISPLACEMENT);
}
/**
* Google api callback methods
*/
@Override
public void onConnectionFailed (ConnectionResult result) {
}
@Override
public void onConnected (Bundle arg0) {
/* try {
double lat = LocationServices.FusedLocationApi.getLastLocation (mGoogleApiClient).getLatitude ();
double lng = LocationServices.FusedLocationApi.getLastLocation (mGoogleApiClient).getLongitude ();
//Utils.logError ("lng", lng + "");
//Utils.logError ("lat", lat + "");
} catch (NullPointerException ignored) {
}*/
startLocationUpdates ();
}
@Override
public void onConnectionSuspended (int arg0) {
mGoogleApiClient.connect ();
}
}
//add this to manifest
<service
android:name=".LocationListenerService"
android:enabled="true"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment