位置客户端使用 PendingIntent 中的可打包附加请求位置更新 [英] Location Client request location updates with parcelable extras in PendingIntent

查看:38
本文介绍了位置客户端使用 PendingIntent 中的可打包附加请求位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 PendingIntent 的 LocationClient 来获取位置更新.

I am using LocationClient with PendingIntent to get location updates.

PendingIntent.getService(context, 0, new Intent(context, OnLocationAvail.class), PendingIntent.FLAG_UPDATE_CURRENT)

上述代码工作正常我从关键 LocationClient.KEY_LOCATION_CHANGED 获取位置

The Above code works fine I get the location from the key LocationClient.KEY_LOCATION_CHANGED

但是,当我有如下所述的可打包数据的额外内容时,该服务将使用可打包数据调用,但意图额外内容中的关键 LocationClient.KEY_LOCATION_CHANGED 始终为空.

But when I have an extras of parcelable data as described below, the service gets called with the parcelable data but the key LocationClient.KEY_LOCATION_CHANGED in the intent extras is always null.

Intent callbackIntent = new Intent(context, OnLocationAvail.class);
callbackIntent.putExtra(SOME_KEY, PARCELABLE_DATA);
PendingIntent.getService(context, 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

推荐答案

问题是当 LocationClient 通过回调 PendingIntent 推送更新时,该进程没有 ClassLoader 的任何信息,因为它发生在应用程序上下文之外并且在不同的过程中.

The problem was when the LocationClient pushes an update via the callback PendingIntent, the process doesn't has any information of the ClassLoader since it is happening outside the application context and in a different process.

以下链接帮助我缩小了问题的范围.https://code.google.com/p/android/issues/detail?id=6822

The following link helped me to narrow down the issue. https://code.google.com/p/android/issues/detail?id=6822

解决方案:

在请求位置更新时使用 hack 包.

Use a hack bundle when requesting for an location update.

Bundle bundle = new Bundle();
bundle.putParcelable("com.foo.parcel", some_parcel);
callbackPendingIntent.putExtra("com.foo.bundle",bundle);

接收位置更新时.

Bundle oldBundle = intent.getBundleExtra("com.foo.bundle");      
some_parcel = oldBundle.getParcelable("com.foo.parcel");

这篇关于位置客户端使用 PendingIntent 中的可打包附加请求位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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