Android:使用 LocationManager.requestLocationUpdates() 时如何从 Intent Bundle Extras 获取位置信息
我正在尝试使用 Android 的 LocationManager requestLocationUpdates。一切正常,直到我尝试提取广播接收器中的实际位置对象。我是否需要专门为我的自定义意图定义“额外”,以便 Android LocationManager 在将其传递给 requestLocationUpdates 之前,以便它知道如何将其添加到意图中,或者无论它何时传递触发,它都会创建额外捆绑包广播接收者的意图?
我的代码如下所示:
Intent intent = new Intent("com.myapp.swarm.LOCATION_READY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//Register for broadcast intents
int minTime = 5000;
int minDistance = 0;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, pendingIntent);
我有一个广播接收器,在宣言中定义为:
<receiver android:name=".LocationReceiver">
<intent-filter>
<action android:name="com.myapp.swarm.LOCATION_READY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
广播接收器类为:
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get("KEY_LOCATION_CHANGED");
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
我的“loc”对象即将为空。
I am trying to use Android's LocationManager requestLocationUpdates. Everything is working until I try to extract the actual location object that in my broadcast receiver. Do I need to specifically define the "extras" to my custom intent so that the Android LocationManager before I pass it to requestLocationUpdates so it knows how to add it into the intent, or will it create the extras-bundle regardless when it passes the fired intent to the broadcast receiver?
My code looks like this:
Intent intent = new Intent("com.myapp.swarm.LOCATION_READY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//Register for broadcast intents
int minTime = 5000;
int minDistance = 0;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, pendingIntent);
I have a broadcast receiver that is defined in the manifesto as:
<receiver android:name=".LocationReceiver">
<intent-filter>
<action android:name="com.myapp.swarm.LOCATION_READY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And the broadcast receiver class as:
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get("KEY_LOCATION_CHANGED");
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
My "loc" object is coming up null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我设法通过将广播接收器代码中的 KEY_LOCATION_CHANGED 更改为:
OK, I managed to fix it by changing the KEY_LOCATION_CHANGED in the broadcast receiver code to:
我尝试编码和测试您提出的解决方案,因为我面临着有关邻近警报和携带位置对象的意图的类似问题。根据您提供的信息,您成功地克服了 BroadcastReceiver 端的空对象检索问题。您可能没有观察到的是,现在您应该接收与第一次创建意图的位置相同的位置(也视为:意图缓存问题)。
为了克服这个问题,我使用了 FLAG_CANCEL_CURRENT,正如这里许多人建议的那样,它工作得很好,获取新鲜(和多汁的:P)位置值。因此,定义您的待处理意图的行应该如下所示:
时接收位置值,则您可以忽略这一点
i ve tried to code and test the solution you proposed, since i am facing similar problems concerning proximity alerts and intents carrying location objects. According the information you provided, you managed to overcome the null object retrieval, on the BroadcastReceiver's side. What you might did not observe is that now you should be receiving the same location as the one your intent was first created (also seen it as: intent caching problem).
In order to overcome this problem, i used FLAG_CANCEL_CURRENT, as being proposed by many people here and it works pretty fine, fetching fresh (and juicy :P) location values. So the line defining your pending intent should look like this:
However, you can ignore this if: