简体   繁体   中英

RequestLocationUpdates with PendingIntent never fires

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?

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() with getService()`.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM