Created
May 6, 2015 06:36
-
-
Save allenchi/c9659369c306752c0047 to your computer and use it in GitHub Desktop.
Fused Location API
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
/** | |
* Location Intent Service | |
*/ | |
import android.app.IntentService; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.location.Location; | |
import android.support.v4.content.LocalBroadcastManager; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.FusedLocationProviderApi; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.maps.model.LatLng; | |
import java.util.concurrent.TimeUnit; | |
import static com.google.android.gms.location.LocationServices.FusedLocationApi; | |
/** | |
* This service runs in the background and broadcast the result to any registered receivers | |
*/ | |
public class LocationService extends IntentService { | |
private static final String TAG = LocationService.class.getSimpleName(); | |
private static final String ACTION_LOCATION_UPDATED = "location_updated"; | |
private static final String ACTION_REQUEST_LOCATION = "request_location"; | |
public static IntentFilter getLocationUpdatedIntentFilter() { | |
return new IntentFilter(LocationService.ACTION_LOCATION_UPDATED); | |
} | |
public static void requestLocation(Context context) { | |
Intent intent = new Intent(context, LocationService.class); | |
intent.setAction(LocationService.ACTION_REQUEST_LOCATION); | |
context.startService(intent); | |
} | |
public LocationService() { | |
super(TAG); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
String action = intent != null ? intent.getAction() : null; | |
if (ACTION_REQUEST_LOCATION.equals(action)) { | |
onRequestLocation(); | |
} else (ACTION_LOCATION_UPDATED.equals(action)) { | |
onLocationUpdated(intent); | |
} | |
} | |
/** | |
* Called when a location update is requested. We block until we get a result back. | |
* We are using Fused Location Api. | |
*/ | |
private void onRequestLocation() { | |
Log.v(TAG, ACTION_REQUEST_LOCATION); | |
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) | |
.addApi(LocationServices.API) | |
.build(); | |
// we block here | |
ConnectionResult connectionResult = googleApiClient.blockingConnect(10, TimeUnit.SECONDS); | |
if (connectionResult.isSuccess() && googleApiClient.isConnected()) { | |
Intent locationUpdatedIntent = new Intent(this, LocationService.class); | |
locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED); | |
// Send last known location out first if available | |
Location location = FusedLocationApi.getLastLocation(googleApiClient); | |
if (location != null) { | |
Intent lastLocationIntent = new Intent(locationUpdatedIntent); | |
lastLocationIntent.putExtra( | |
FusedLocationProviderApi.KEY_LOCATION_CHANGED, location); | |
startService(lastLocationIntent); | |
} | |
// Request new location | |
LocationRequest mLocationRequest = new LocationRequest() | |
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
FusedLocationApi.requestLocationUpdates( | |
googleApiClient, mLocationRequest, | |
PendingIntent.getService(this, 0, locationUpdatedIntent, 0)); | |
googleApiClient.disconnect(); | |
} else { | |
Log.e(TAG, String.format("Failed to connect to GoogleApiClient (error code = %d)", | |
connectionResult.getErrorCode())); | |
} | |
} | |
/** | |
* Called when the location has been updated & broadcast the new location | |
*/ | |
private void onLocationUpdated(Intent intent) { | |
Log.v(TAG, ACTION_LOCATION_UPDATED); | |
// Extra new location | |
Location location = | |
intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); | |
if (location != null) { | |
LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude()); | |
LocalBroadcastManager.getInstance(this).sendBroadcast(intent); | |
} | |
} | |
} | |
// On the activity/fragment | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if(checkPlayServices()) { | |
LocationService.requestLocation(this); | |
LocalBroadcastManager.getInstance(getActivity()) | |
.registerReceiver(locationReceiver, LocationService.getLocationUpdatedIntentFilter()); | |
} | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
LocalBroadcastManager.getInstance(getActivity()).registerReceiver( | |
locationReceiver, LocationService.getLocationUpdatedIntentFilter()); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(locationReceiver); | |
} | |
private BroadcastReceiver locationReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); | |
if (location != null) { | |
LatLng latestLocation = new LatLng(location.getLatitude(), location.getLongitude()); | |
// do something with the location | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment