Skip to content

Instantly share code, notes, and snippets.

@xizzhu
Created February 8, 2014 10:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xizzhu/8881438 to your computer and use it in GitHub Desktop.
Save xizzhu/8881438 to your computer and use it in GitHub Desktop.
Locations
public class MainActivity extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener,
LocationClient.OnAddGeofencesResultListener {
private LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(this, this, this);
}
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
public void onConnected(Bundle dataBundle) {
// this callback will be invoked when the client is connected
// from now on, you can fetch current location and listen to location updates
// fetch the current location
// note that this method might return null in a very rare case (i.e. location is not available)
Location location = mLocationClient.getLastLocation();
// start listening to location updates
// this is suitable for foreground listening, with the onLocationChanged() invoked for location updates
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setFastestInterval(5000L)
.setInterval(10000L)
.setSmallestDisplacement(75.0F);
mLocationClient.requestLocationUpdates(locationRequest, this);
// this is suitable for background location tracking
// your MyLocationHandler service will still be triggered even if your app is killed by the system
PendingIntent pendingIntent = PendingIntent.getService(this, 0,
new Intent(this, MyLocationHandler.class),
PendingIntent.FLAG_UPDATE_CURRENT);
mLocationClient.requestLocationUpdates(locationRequest, pendingIntent);
// adds geofencing
ArrayList<Geofence> geofences = new ArrayList<Geofence>();
geofences.add(new Geofence.Builder()
.setRequestId("unique-geofence-id")
.setCircularRegion(60.1708, 24.9375, 1000)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT)
.setLoiteringDelay(30000) // GEOFENCE_TRANSITION_DWELL will be notified 30 seconds after the device enters the area
.build());
PendingIntent pendingIntent2 = PendingIntent.getService(this, 0,
new Intent(this, MyGeofenceHandler.class),
PendingIntent.FLAG_UPDATE_CURRENT);
mLocationClient.addGeofences(geofences, pendingIntent2, this);
}
@Override
public void onDisconnected() {
// this callback will be invoked when the client is disconnected
// it might happen e.g. when the location service crashes
// you can't access until it's connected again
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// this callback will be invoked when the connection attempt fails
if (connectionResult.hasResolution()) {
// Google Play services can fix the issue
try {
connectionResult.startResolutionForResult(this, 0);
} catch (IntentSender.SendIntentException e) {
// it happens if the resolution intent has been canceled,
// or is no longer able to execute the request.
}
} else {
// Google Play services has no idea how to fix the issue
// it rarely happens for the location service
}
}
@Override
public void onLocationChanged(Location location) {
// this callback is invoked when location updates
}
@Override
public void onAddGeofencesResult (int statusCode, String[] geofenceRequestIds) {
// this callback is invoked when geofences are added, either succeed or fail
}
@Override
protected void onStop() {
if (mLocationClient.isConnected())
mLocationClient.removeLocationUpdates(this);
mLocationClient.disconnect();
super.onStop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment