onHandleIntent not firing with location updates

219 views Asked by At

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();
        }
    }
}
1

There are 1 answers

1
CommonsWare On BEST ANSWER

Can you tell me is there something I'm doing wrong?

First, a Service is a Context. You do not need to — or even want to — pass in some Context to it.

Second, your constructor should never be used. Replace it with a zero-argument constructor.

Third, start() and stop() are not lifecycle methods on an IntentService, and so I am not quite certain what you are expecting will call them. That, coupled with the previous problem, means nothing will really happen with this service.

Saying that you want an IntentService to handle the locations, via an onHandleIntent() method, is reasonable. However, something outside of that service is going to need to do the connecting and disconnecting, or you are going to need a substantially more complex service.