Skip to content

Instantly share code, notes, and snippets.

Created April 7, 2015 05:06
Show Gist options
  • Save anonymous/214f8fe6948314a2d043 to your computer and use it in GitHub Desktop.
Save anonymous/214f8fe6948314a2d043 to your computer and use it in GitHub Desktop.
public class BackgroundLocationService extends
Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
IBinder mBinder = new LocalBinder();
protected GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private boolean mInProgress;
private Boolean servicesAvailable = false;
private String TAG = BackgroundLocationService.class.getName();
private Utils utils;
public class LocalBinder extends Binder {
public BackgroundLocationService getServerInstance() {
return BackgroundLocationService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
mInProgress = false;
servicesAvailable = servicesConnected();
utils = new Utils(getApplicationContext());
Log.i(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(5 * 60 * 1000);
mLocationRequest.setPriority
(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
private boolean servicesConnected() {
int resultCode = GooglePlayServicesUtil.
isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (!servicesAvailable || mGoogleApiClient.isConnected() || mInProgress)
return START_STICKY;
setUpLocationClientIfNeeded();
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) {
mInProgress = true;
mGoogleApiClient.connect();
}
return START_STICKY;
}
private void setUpLocationClientIfNeeded() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
startLocationUpdates();
}
}
@Override
public void onLocationChanged(Location location) {
String msg = Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Log.d("debug", msg);
JSONObject locationJson = new JSONObject();
try {
locationJson.put("latitude", location.getLatitude());
locationJson.put("longitude", location.getLongitude());
} catch (JSONException e) {
e.printStackTrace();
}
utils.setPreference(Constants.CURRENT_LOCATION, locationJson.toString());
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onDestroy() {
mInProgress = false;
if (servicesAvailable && mGoogleApiClient != null) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
// Destroy the current location client
mGoogleApiClient = null;
}
super.onDestroy();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
stopLocationUpdates();
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind()");
super.onRebind(intent);
if (mGoogleApiClient.isConnected() && mInProgress) {
startLocationUpdates();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mInProgress = false;
if (connectionResult.hasResolution()) {
stopLocationUpdates();
} else {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment