Android FusedLocationProviderApi:传入意图没有LocationResult或LocationAvailability

fps*_*li3 10 android location

我正在尝试通过Google的FusedLocationProviderApi订阅位置更新.我想在后台接收更新,这样即使应用程序被杀,我也会收到更新.尽可能遵循在线文档,我编写了以下代码.注意:这是在intent服务中完成的,而不是在UI线程上完成的,这就是我使用阻塞连接/结果方法的原因.

private void startLocationServices(String deviceId, int pollingInterval) {
    Log.i(TAG, "Starting location services with interval: " + pollingInterval + "ms");
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.acquire();

    final GoogleApiClient googleApiClient =
            new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .build();

    ConnectionResult result = googleApiClient.blockingConnect();

    if (!result.isSuccess() || !googleApiClient.isConnected()) {
        Log.e(TAG, "Failed to connect to Google Api");
        wakeLock.release();
        return;
    }

    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(pollingInterval);
    locationRequest.setFastestInterval(10000);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    Intent locationIntent = new Intent(this, GeoBroadcastReceiver.class);
    locationIntent.putExtra(EXTRA_LOCATION_UPDATE_DEVICE_ID, deviceId);
    locationIntent.setAction(GeoBroadcastReceiver.ACTION_LOCATION_UPDATE);
    PendingIntent locationPendingIntent = PendingIntent.getBroadcast(
            this, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    PendingResult pendingResult = LocationServices.FusedLocationApi
            .requestLocationUpdates(googleApiClient, locationRequest, locationPendingIntent);

    Result requestResult = pendingResult.await();
    Status requestStatus = requestResult.getStatus();

    if (requestStatus.isSuccess()) {
        Log.i(TAG, "Successfully subscribed to location updates.");
    } else {
        Log.e(TAG, String.format(
                        "Failed subscribe to location updates. Error code: %d, Message: %s.",
                        requestStatus.getStatusCode(),
                        requestStatus.getStatusMessage()));
    }

    googleApiClient.disconnect();
    wakeLock.release();
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到requestStatus.isSuccess()返回true,表明我已成功订阅了位置更新.此外,GeoBroadcastReciever扩展的The ,WakefulBroadcastReceiver以正确的轮询间隔接收意图,并采取正确的操作.到目前为止看起来很好.这是我在以下onReceive方法中所做的GeoBroadcastReceiver:

    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();
        if (location != null) {
            GeoMonitoringService.wakefulLocationUpdate(context, location);
        } else {
            Log.e(TAG, "LocationResult does not contain a LastLocation.");
        }
    } else {
        Log.e(TAG, "Intent does not contain a LocationResult.");
    }
Run Code Online (Sandbox Code Playgroud)

问题是,无论何时意图进入,它都不包含LocationResult,也不包含LocationAvailabilityResult.我在调试器中检查了传入的意图,而intent的附加内容中唯一的项是我在设置intent(设备ID)时添加的额外项.因此,LocationResult.hasResult()返回false.每一次.

我在运行4.0.1的Galaxy Note 4和运行5.1.1的Nexus 4上尝试了这个,结果相同.

如果我在手机上禁用了位置,我会按预期完全停止接收意图.

Fer*_*ego 10

从待处理的意图中删除额外内容,否则不会传递位置结果.我无法在文档中找到这个解释的位置,但经过大量的试验和错误后我发现了.

  • 我为此提交了 [错误报告](https://issuetracker.google.com/issues/119995978)。 (2认同)

小智 2

解决方法(Christophe Beyls 建议只使用 Intent Data)因此,由于我只需要发送一些参数,所以我做了这样的事情:在 requestLocationUpdates 之前构建 Intent 时:intent.setData(Uri.parse(" http://a.com/a ?"+ Param1+ "?" + Param2+ "?" + Param3); 并在 BroadcastReceiver 中: String[]parameters = intent.getDataString().split("[?]");这工作正常,intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED) 确实返回位置。