简体   繁体   English

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

[英]Android: How to find out location provider name from KEY_STATUS_CHANGED broadcast intent sent by LocationManager?

I try work with NETWORK and GPS location provider simultaneously. 我尝试同时使用NETWORK和GPS位置提供程序。 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: 在我的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";
    }

}

Question is: how to determine from which provider arrived this intent? 问题是:如何确定这个意图是从哪个提供者到达的? (String provider_name = ???;) (字符串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... 但未成功:提供商的传入意图会擦除我的数据...

I found solution. 我找到了解决方案。 It was a misunderstanding how Android work with PendingIntents. Android如何与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. 确实使用有效负载额外功能的解决方案正在起作用,但是如果以前曾提出过一些对没有有效负载数据的PendingIntents的请求,则应用程序会收到没有有效负载数据的旧意图。 It is necessary reload device or make new request for PendingIntent with flag: 必须重新加载设备或使用标志对PendingIntent发出新请求:

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

After that incoming intent will carry information about provider name. 之后,传入意图将携带有关提供者名称的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android locationManager-“位置未更改”事件 - Android locationManager - “location not changed” event Android:使用LocationManager.requestLocationUpdates()时如何从intent bundle extras获取位置信息 - Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates() 如何从Android中的LocationManager获取位置 - How to get Location from LocationManager in android 如何从android中广播接收器收到的意图中获取应用程序名称 - how to get application name from the intent received in the broadcast receiver in android 具有待定意图的RequestLocationUpdate以及如何获取位置提供者(GPS,网络)状态? - RequestLocationUpdate with Pending Intent and how to get the Location Provider(GPS, Network) status? 没有网络的Android Google Maps位置与通过网络提供商从LocationManager检索到的GPS与位置的对比 - Android Google Maps location without GPS vs location retrieved from LocationManager with network provider 如何使用广播接收器从 smsmanager (nativescript) 获取发送的意图 - How to use broadcast reciever to get sent intent from smsmanager (nativescript) Android:如何从本机代码广播意图? - Android: How to broadcast intent from native code? 如何找出其他应用的广播意图动作? - How can find out the broadcast intent actions of other apps? 接收广播意图时出错{act = android.location.PROVIDERS_CHANGED flg = 0x10} - Error receiving broadcast Intent { act=android.location.PROVIDERS_CHANGED flg=0x10 }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM