Skip to content

Instantly share code, notes, and snippets.

@TimCastelijns
Created February 17, 2017 15:17
Show Gist options
  • Save TimCastelijns/b19051dcbea4adc56eb4e51c76ddbcc4 to your computer and use it in GitHub Desktop.
Save TimCastelijns/b19051dcbea4adc56eb4e51c76ddbcc4 to your computer and use it in GitHub Desktop.
requesting location updates using IntentService
public class LocationUpdateService extends IntentService {
private Realm realm;
public LocationUpdateService() {
super("LocationUpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
WakeLock wakeLock = WakeLock.getInstance(this);
wakeLock.acquire();
Logger.log(LogLevel.INFO, "got a new location", "background service running: " + BackgroundService.isRunning());
if (!LocationResult.hasResult(intent)) {
Logger.log(LogLevel.ERROR, "LocationUpdateService fired, but a LocationResult is missing", "");
wakeLock.release();
return;
}
LocationResult locationResult = LocationResult.extractResult(intent);
if (!(locationResult.getLocations().size() > 0)) {
Logger.log(LogLevel.ERROR, "LocationUpdateService fired, but no location(s) available", "");
wakeLock.release();
return;
}
Location location = locationResult.getLastLocation();
Logger.log(LogLevel.INFO, "got new location", "lat: " + location.getLatitude() + " - long: " + location.getLongitude());
realm = Realm.getDefaultInstance();
storeLocation(location);
realm.close();
wakeLock.release();
}
public class LocationUtils {
public static final int MAX_LOCATIONS_PER_MINUTE = 2;
/**
* Requests location updates.
*/
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
@SuppressWarnings("MissingPermission")
public static void requestLocationUpdates(Context context, GoogleApiClient googleApiClient) throws IllegalStateException {
try {
final LocationRequest locationRequest = getLocationRequest();
LocationServices.FusedLocationApi
.requestLocationUpdates(googleApiClient, locationRequest, getPendingIntentLocationUpdates(context));
Logger.log(LogLevel.INFO, "location updates successfully requested", "");
} catch (SecurityException ex) {
Logger.log(LogLevel.INFO, "requestLocationUpdates failed", "ex: " + ex.getMessage());
}
}
/**
* Stops location updates.
*/
public static void stopLocationUpdates(Context context, GoogleApiClient googleApiClient) throws IllegalStateException {
LocationServices.FusedLocationApi
.removeLocationUpdates(googleApiClient, getPendingIntentLocationUpdates(context));
Logger.log(LogLevel.INFO, "location updates successfully stopped", "");
}
/**
* Creates and returns a LocationRequest that is used to configure the location updates.
*
* It requests to actively receive locations every 60 seconds. However if other apps are also
* fixing locations, they will get broadcasted to us as well. With setFastestInterval() we can
* set a lower limit for receiving updates. Currently we will not handle updates faster than one
* per 30 seconds.
*
*/
private static LocationRequest getLocationRequest() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(Interval.MILLIS_MINUTE);
locationRequest.setFastestInterval(Interval.MILLIS_30_SEC);
return locationRequest;
}
/**
* Creates and returns a PendingIntent for {@link xxx.xxx.xxx.services.intent.LocationUpdateService}
*/
private static PendingIntent getPendingIntentLocationUpdates(Context context) {
return PendingIntent.getService(
context,
0,
new Intent(context, LocationUpdateService.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
}
/**
* Returns the best most recent location currently available.
* Returns null if a location is not available, or of the googleApiClient is not connected.
*/
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
@SuppressWarnings("MissingPermission")
@Nullable
public static Location getLastLocation() {
return LocationServices.FusedLocationApi.getLastLocation(BackgroundService.getGoogleApiClient());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment