Android:使用 LocationManager.requestLocationUpdates() 时如何从 Intent Bundle Extras 获取位置信息

发布于 2024-08-16 12:06:22 字数 1340 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-08-23 12:06:22

好的,我设法通过将广播接收器代码中的 KEY_LOCATION_CHANGED 更改为:

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(android.location.LocationManager.KEY_LOCATION_CHANGED);

    Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); 
    }
}

OK, I managed to fix it by changing the KEY_LOCATION_CHANGED in the broadcast receiver code to:

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(android.location.LocationManager.KEY_LOCATION_CHANGED);

    Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); 
    }
}
可爱暴击 2024-08-23 12:06:22

我尝试编码和测试您提出的解决方案,因为我面临着有关邻近警报和携带位置对象的意图的类似问题。根据您提供的信息,您成功地克服了 BroadcastReceiver 端的空对象检索问题。您可能没有观察到的是,现在您应该接收与第一次创建意图的位置相同的位置(也视为:意图缓存问题)。

为了克服这个问题,我使用了 FLAG_CANCEL_CURRENT,正如这里许多人建议的那样,它工作得很好,获取新鲜(和多汁的:P)位置值。因此,定义您的待处理意图的行应该如下所示:

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

时接收位置值,则您可以忽略这一点

  • 但是,如果您的目的只是
  • 在您设法以帖子中不可见的其他方式克服它

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:

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

However, you can ignore this if:

  • your purpose was just to receive a location value once
  • you managed to overcome it in some other way not visible in your post
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文