FusedLocationProvider不间断发送pendingIntents

时间:2017-08-07 12:23:09

标签: android location android-pendingintent fusedlocationproviderapi

Android设备监视器:

Non stop intents

我的位置更新服务每秒接收数千个意图。我不确定是什么触发了它

实例化并调用Fused Location,我实施了GoogleApiClient.ConnectionCallbacks,因此我可以在收到onConnected时启动位置请求

private GoogleApiClient mGoogleApiClient;
private boolean mInProgress = false;
private static int INTERVAL = 10*60*1000;
private LocationRequest mLocationRequest;

public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setSmallestDisplacement(20)
            .setFastestInterval(INTERVAL)
            .setInterval(INTERVAL);

    intent = new Intent(this.getApplicationContext(), LocationUpdateService.class);
    pendingIntent = PendingIntent.getService(this.getApplicationContext(), 0, intent, 0);

    Log.d(TAG, "on create");
}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "Connected to FusedLocationAPI");
    mInProgress = false;
    Log.d(TAG, "Fastest interval : " + mLocationRequest.getFastestInterval());
    Log.d(TAG, "interval : " + mLocationRequest.getInterval());

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, pendingIntent);

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location != null) Log.d(TAG, location.toString());
}

收到位置的我的更新服务很简单,它会调用其他服务来处理位置数据

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();

        sIntent = new Intent(this, DoWork.class);

        sIntent.putExtra("LATITUDE", location.getLatitude());
        sIntent.putExtra("LONGITUDE", location.getLongitude());

        startService(intent);

        Log.d(TAG, "onStartCommand has Intent : " + startId);
        stopSelf(startId);
    } else {
        Log.d(TAG, "onStartCommand no Intent");
    }

    return START_STICKY;
}

如果我注释掉LocationServices.FusedLocationApi.requestLocationUpdates行,则意图不会按预期触发,因此它让我相信请求构造不当或我没有正确处理意图以表示我已处理它,我找不到任何与这些

相关的东西

1 个答案:

答案 0 :(得分:0)

您是否尝试更改PRIORITY_BALANCED_POWER_ACCURACYPRIORITY_LOW_POWER的setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)? 我也使用intentService并在onHandleIntent

中覆盖它
    @Override
    protected void onHandleIntent(Intent intent) {
    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());

        if (location != null) {
            Log.d("locationtesting", "accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude());
        }
    }
}