Android FusedLocationProviderApi:传入意图没有LocationResult或LocationAvailability [英] Android FusedLocationProviderApi: Incoming intent has no LocationResult or LocationAvailability

查看:230
本文介绍了Android FusedLocationProviderApi:传入意图没有LocationResult或LocationAvailability的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to subscribe to location updates via Google's FusedLocationProviderApi. I want to receive updates in the background, so that I will receive updates even if the app is killed. Following the online documentation as best as I can, I've written the following code. Note: this is being done in an intent service, not on the UI thread, which is why I'm using blocking connect/result methods.

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

运行此命令时,我看到requestStatus.isSuccess()返回true,表示我已经成功订阅了位置更新.此外,扩展了WakefulBroadcastReceiverGeoBroadcastReciever以正确的轮询间隔和正确的动作接收到一个意图.到目前为止看来还不错.这是我在GeoBroadcastReceiveronReceive方法中所做的事情:

When I run this, I see that requestStatus.isSuccess() returns true, indicating that I've successfully subscribed to the location updates. Additionally, The GeoBroadcastReciever, which extends WakefulBroadcastReceiver, receives an intent at the correct polling interval, with the correct action. Good so far, it would seem. Here is what I'm doing in the onReceive method for the 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.");
    }

问题是,只要有意图,它就不包含LocationResult,也不包含LocationAvailabilityResult.我检查了调试器中的传入意图,并且意图附加项中的唯一项是设置意图(设备ID)时添加的附加项.这样,LocationResult.hasResult()返回false.每一次.

The problem is, whenever the intent comes in, it does not contain the LocationResult, nor does it contain the LocationAvailabilityResult. I inspected the incoming intent in the debugger, and the only item in the intent's extras is the extra I added when setting up the intent (the device id). As such, LocationResult.hasResult() returns false. Every single time.

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

I've tried this on a Galaxy Note 4 running 4.0.1, and a Nexus 4 running 5.1.1, with the same result.

如果我禁用了手机上的位置信息,那么我将完全停止接收意图,

If I disable location on the phone, I stop receiving intents altogether, as expected.

推荐答案

从挂起的意图中删除多余内容,否则将无法提供位置结果.我在文档中找不到解释的地方,但是经过反复试验后才发现.

Remove the extras from the pending intent, otherwise the location result is not delivered. I can't find where in the documentation this is explained but I found out after lot of trial and error.

这篇关于Android FusedLocationProviderApi:传入意图没有LocationResult或LocationAvailability的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆