I am trying to listen to location changes using the Play Services Location API.
What I'm trying to do is go for the background app approach and get the updates using a PendingIntent
. However, the onHandleIntent() function does not get called at all.
I didn't find a single source of comprehensive documentation regarding this approach. Can you tell me is there something I'm doing wrong?
public class LocationCollector extends IntentService implements GoogleApiClient.ConnectionCallbacks
, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mLocationClient;
public LocationCollector(Context context){
super("LocationCollector");
mContext = context;
mLocationClient = new GoogleApiClient.Builder(mContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void start(){
mLocationClient.connect();
}
public void stop(){
mLocationClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
LocationRequest request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Intent locationIntent = new Intent(mContext, LocationCollector.class);
PendingIntent locationPendingIntent = PendingIntent.getService(mContext, 1, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingResult<Status> result = LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, request, locationPendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
Log.d("Location","Api connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Location","Api connection failed");
}
@Override
protected void onHandleIntent(Intent intent) {
Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
if(location != null){
String time= intent.getStringExtra("time");
Toast.makeText(mContext,location.toString(),Toast.LENGTH_SHORT).show();
}
}
}