Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何从LocationManager发送的密钥\状态\更改的广播意图中找到位置提供商名称?_Android_Geolocation - Fatal编程技术网

Android:如何从LocationManager发送的密钥\状态\更改的广播意图中找到位置提供商名称?

Android:如何从LocationManager发送的密钥\状态\更改的广播意图中找到位置提供商名称?,android,geolocation,Android,Geolocation,我尝试同时与网络和GPS定位提供商合作。在主活动中,我为每个可用的提供者注册位置更新请求: for (String provider : m_lm.getProviders(false)) { Intent intent = new Intent(GpsMain.this, GpsTestReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(GpsMain.th

我尝试同时与网络和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);
        }
在my BroadcastReceiver(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";
    }

}
问题是:如何确定这一意图是由哪个提供商提出的?(字符串提供程序名称=???;)

我尝试在注册时将intent provider名称作为有效负载包括在内,如下所示:

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

但未成功:来自提供商的传入意图擦除我的数据…

我找到了解决方案。这是一个关于Android如何与PendingIntents一起工作的误解。事实上,使用payload extra的解决方案是可行的,但若之前已经发出了一些不带有效负载数据的PendingEvents请求,那个么应用程序将收到不带有效负载数据的旧意图。有必要重新加载设备或使用标志重新请求PendingEvent:

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

在此之后,传入的意图将包含有关提供商名称的信息。

我找到了解决方案。这是一个关于Android如何与PendingIntents一起工作的误解。事实上,使用payload extra的解决方案是可行的,但若之前已经发出了一些不带有效负载数据的PendingEvents请求,那个么应用程序将收到不带有效负载数据的旧意图。有必要重新加载设备或使用标志重新请求PendingEvent:

            PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
之后,传入的意图将携带有关提供者名称的信息