我正在尝试通过谷歌的FusedLocationProviderApi订阅位置更新。我希望在后台接收更新,这样即使应用程序被终止,我也会收到更新。根据在线文档,我尽可能地编写了以下代码。注意:这是在意图服务中完成的,而不是在UI线程上完成的,这就是我使用阻塞connect/result方法的原因。
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,这表明我已经成功订阅了位置更新。此外,扩展WakefulBroadcastReceiver的GeoBroadcastReciever以正确的轮询间隔和正确的操作接收意图。到目前为止,看起来还不错。下面是我在GeoBroadcastReceiver的onReceive方法中所做的事情
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。我在调试器中检查了传入的intent,intent的附加内容中唯一的一项是我在设置intent时添加的额外内容(设备id)。因此,LocationResult.hasResult()返回false。每一次。
我在运行4.0.1的Galaxy Note 4和运行5.1.1的Nexus4上进行了测试,结果相同。
如果我禁用电话上的定位,我将完全停止接收意向,正如预期的那样。
发布于 2016-11-23 20:22:07
从挂起的intent中删除多余的内容,否则定位结果不会下发。我在文档中找不到这一点的解释,但我在经过多次试验和错误后找到了答案。
发布于 2016-02-09 23:34:31
一个变通方法(Christophe Beyls建议只使用Intent数据)所以,因为我只需要发送几个参数,所以我这样做:在requestLocationUpdates之前构建Intent时:intent.setData(“http://a.com/a?”+ Param1+ "?“+ Param2+ "?”+ Param3);在BroadcastReceiver中: String[] parameters = intent.getDataString().split("");这很好用,并且Intent确实返回位置。
发布于 2016-03-18 18:20:52
您可以使用:
int id = 7;
String name = "myName";
uriBuilder.scheme("http")
.authority("workaround.com")
.appendPath("extra")
.appendQueryParameter("id", String.valueOf(id))
.appendQueryParameter("name", name);
intent.setData(uriBuilder.build());和
@Override
protected void onHandleIntent(Intent intent) {
if (LocationResult.hasResult(intent)) {
int id = Integer.valueOf(uri.getQueryParameter("id"));
String name = uri.getQueryParameter("name");
....
}
}https://stackoverflow.com/questions/32893006
复制相似问题