gpt4 book ai didi

Android FusedLocationProviderApi : Incoming intent has no LocationResult or LocationAvailability

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:33 26 4
gpt4 key购买 nike

我正在尝试通过 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();
}

当我运行它时,我看到 requestStatus.isSuccess() 返回 true,表明我已成功订阅位置更新。此外,扩展了 WakefulBroadcastReceiverGeoBroadcastReciever 以正确的轮询间隔接收 Intent ,并执行正确的操作。到目前为止还不错,看起来。以下是我在 GeoBroadcastReceiveronReceive 方法中所做的:

    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.");
}

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

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

如果我在手机上禁用定位功能,我就会像预期的那样完全停止接收 Intent 。

最佳答案

从pending intent中移除extras,否则定位结果不送达。我无法在文档中找到对此进行解释的位置,但经过大量试验和错误后我发现了。

关于Android FusedLocationProviderApi : Incoming intent has no LocationResult or LocationAvailability,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com