【问题标题】:Android: How to find out location provider name from KEY_STATUS_CHANGED broadcast intent sent by LocationManager?Android:如何从 LocationManager 发送的 KEY_STATUS_CHANGED 广播意图中找出位置提供者名称?
【发布时间】:2010-07-11 00:27:05
【问题描述】:

我尝试同时与 NETWORK 和 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);
        }

在我的 BroadcastReceiver (GpsTestReceiver) 中进行传入 Intent 处理:

@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";
    }

}

问题是:如何确定这个意图来自哪个提供者? (字符串 provider_name = ???;)

我尝试在注册时将意图提供者名称作为有效负载包含在这样的负载中:

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

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

【问题讨论】:

    标签: android geolocation


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多