Created
April 22, 2016 11:47
-
-
Save tassioauad/027e0350f8b3751ec373dbab2799dd32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tassioauad.example.location; | |
import android.content.Context; | |
import android.location.Location; | |
import android.os.Bundle; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
public class LocationCapturer implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { | |
private static final long LOCATION_CAPTURING_INTERVAL_INMILIS = 10000; | |
private static final long LOCATION_CAPTURING_FASTESTINTERVAL_INMILIS = 5000; | |
private static final long LOCATION_CAPTURING_SMALESTDISPLACEMENT_INMETERS = 5; | |
private static final float DESIRABLE_ACCURANCY = 50f; | |
private GoogleApiClient googleApiClient; | |
public LocationCapturer(Context context) { | |
googleApiClient = new GoogleApiClient.Builder(context) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
//Trying to connect with Google API. If it works onConnected() will be called, else onConnectionFailed() will be called. | |
googleApiClient.connect(); | |
//Stopping the Connection | |
if (googleApiClient.isConnected()) { | |
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); //Stopping to get location updates | |
googleApiClient.disconnect(); //Closing connection with Google APIs | |
} | |
} | |
/** | |
* Called when there was an error connecting the client to the service. | |
* | |
* @param connectionResult | |
*/ | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
//Google API connection has been failed! Stop it and warn! | |
} | |
/** | |
* After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed. | |
* After this callback, the application can make requests on other methods provided by the client and expect that no user intervention | |
* is required to call methods that use account and scopes provided to the client constructor. | |
* | |
* @param bundle | |
*/ | |
@Override | |
public void onConnected(Bundle bundle) { | |
//Google API connection has been done successfully | |
LocationRequest locationRequest = new LocationRequest(); | |
locationRequest.setInterval(LOCATION_CAPTURING_INTERVAL_INMILIS); | |
locationRequest.setFastestInterval(LOCATION_CAPTURING_FASTESTINTERVAL_INMILIS); | |
locationRequest.setSmallestDisplacement(LOCATION_CAPTURING_SMALESTDISPLACEMENT_INMETERS); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); | |
} | |
/** | |
* Called when the client is temporarily in a disconnected state. | |
* This can happen if there is a problem with the remote service (e.g. a crash or resource problem causes it to be killed by the system). | |
* When called, all requests have been canceled and no outstanding listeners will be executed. | |
* GoogleApiClient will automatically attempt to restore the connection. | |
* Applications should disable UI components that require the service, and wait for a call to onConnected(Bundle) to re-enable them. | |
*/ | |
@Override | |
public void onConnectionSuspended(int i) { | |
//Wait for the GoogleApiClient restores the connection. | |
} | |
/** | |
* Called when the location has changed. | |
* | |
* @param location | |
*/ | |
@Override | |
public void onLocationChanged(Location location) { | |
//Current location has been catched! | |
if(location.hasAccuracy() && location.getAccuracy() < DESIRABLE_ACCURANCY) { | |
//Location accuracy is acceptable | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment