2

I'm trying to run a location check every so often in the background, so I'm trying to use a IntentService to get location updates from the FusedLocationApi. But the PendingIntent never fires.

Code:

public class LocationIntentService extends IntentService
   implements
       GoogleApiClient.ConnectionCallbacks,
       GoogleApiClient.OnConnectionFailedListener
{
    private static GoogleApiClient mApiClient;
    ...
    @Override
    public void onCreate() {
        if (mApiClient == null) {
            mApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
            mApiClient.connect();
        }
    }
    ...
    @Override
    public void onConnected(Bundle bundle) {
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(Constants.LOCATION_UPDATE_INTERVAL_MILIS);
        locationRequest.setFastestInterval(Constants.LOCATION_UPDATE_INTERVAL_MILIS / 2);

        Intent locationIntent = new Intent(getApplicationContext(), LocationIntentService.class);
        PendingIntent locationPendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(),
            0,
            locationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

        LocationServices.FusedLocationApi
            .requestLocationUpdates(mApiClient, locationRequest,locationPendingIntent);
        ...
    }
    ...
}

Am I missing something?

h0x0
  • 485
  • 3
  • 11

1 Answers1

4

The PendingIntent you are providing to requestLocationUpdates() will fire a broadcast Intent because you've called PendingIntent.getBroadcast(). However the class you've provided in the PendingIntent is a Service. Replace getBroadcast() withgetService()`.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Am having a similar issue. i use a broadcast and its been called just once of which `LocationResult locationResult = LocationResult.extractResult(intent); returns null` and my broadcast is never fired again – Phil Aug 07 '16 at 04:49
  • @philipoghenerobobalogun you might want to open a new question with your situation. Commenting like this isn't likely to get you any help from the community. – David Wasser Aug 07 '16 at 08:36
  • i've asked a new question on this here http://stackoverflow.com/questions/38814822/fusedlocationapi-with-pendingintent-is-fired-only-once-and-its-null – Phil Aug 07 '16 at 13:58
  • @DavidWasser How do you change on demand the locationRequestObjects values, lets say I start in "normal" rate and for a period of time (30 sec) I want it to be in realtime "fast" rate. Currently I am getting Looper exceptions when I try to do it. – Pedro Varela Mar 01 '17 at 20:54
  • @PedroVarela please open a new question. Asking for this type of clarification in a comment on another question isnt the right way to use this platform. Other with a similar problem won't be able to find the answer. It will also get you better answers and better attention. – David Wasser Mar 02 '17 at 15:57