Android:如何从 LocationManager 发送的 KEY_STATUS_CHANGED 广播意图中查找位置提供商名称?

发布于 2024-09-09 11:12:26 字数 1374 浏览 1 评论 0原文

我尝试同时与网络和 GPS 位置提供商合作。在主要活动中,我为每个可用提供者注册位置更新请求:

        for (String provider : m_lm.getProviders(false)) {
            Intent intent = new Intent(GpsMain.this, GpsTestReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, 0);

            m_lm.requestLocationUpdates(provider, 60000, 10, pi);
        }

在我的广播接收器(GpsTestReceiver)中进行传入意图处理:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle ex = intent.getExtras();
    if (ex.containsKey(LocationManager.KEY_STATUS_CHANGED)) {
        int status = (Integer)ex.get(LocationManager.KEY_STATUS_CHANGED);
        String str_status;
        switch (status) {
        case 1:
            str_status = "TEMPORARILY_UNAVAILABLE";
            break;
        case 2:
            str_status = "AVAILABLE";
            break;
        default:
            str_status = "OUT_OF_SERVICE";
            break;
        }
        String provider_name = ???;
        s = provider_name+": change status into "+str_status+"\n";
    }

}

问题是:如何确定来自哪个提供者到达此意图? (String provider_name = ???;)

我尝试在注册时将意图提供者名称作为有效负载包含在内,如下所示:

                intent.putExtra("local.home.gpstest.PROVIDER", provider);

但不成功:来自提供者的传入意图删除了我的数据...

I try work with NETWORK and GPS location provider simultaneously. In main activity I register location update request for each available provider:

        for (String provider : m_lm.getProviders(false)) {
            Intent intent = new Intent(GpsMain.this, GpsTestReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, 0);

            m_lm.requestLocationUpdates(provider, 60000, 10, pi);
        }

In my BroadcastReceiver (GpsTestReceiver) do incoming Intent handling:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle ex = intent.getExtras();
    if (ex.containsKey(LocationManager.KEY_STATUS_CHANGED)) {
        int status = (Integer)ex.get(LocationManager.KEY_STATUS_CHANGED);
        String str_status;
        switch (status) {
        case 1:
            str_status = "TEMPORARILY_UNAVAILABLE";
            break;
        case 2:
            str_status = "AVAILABLE";
            break;
        default:
            str_status = "OUT_OF_SERVICE";
            break;
        }
        String provider_name = ???;
        s = provider_name+": change status into "+str_status+"\n";
    }

}

Question is: how to determine from which provider arrived this intent? (String provider_name = ???;)

I try at registration moment include in the intent provider name as a payload like this:

                intent.putExtra("local.home.gpstest.PROVIDER", provider);

but unsuccessfully: incoming intent from provider erase my data...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陪你搞怪i 2024-09-16 11:12:26

我找到了解决方案。这是对 Android 如何使用 PendingIntents 的误解。事实上,具有额外负载的解决方案正在工作,但如果之前已经对没有负载数据的 PendingIntents 发出了一些请求,则应用程序会收到没有负载数据的旧意图。有必要重新加载设备或对带有标志的 PendingIntent 发出新请求:

            PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

之后传入的意图将携带有关提供者名称的信息。

I found solution. It was a misunderstanding how Android work with PendingIntents. Indeed solution with payload extra's is working, but if previously has been made some requests for PendingIntents without payload data, application receive old intents without payload data. It is necessary reload device or make new request for PendingIntent with flag:

            PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

After that incoming intent will carry information about provider name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文