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

╄→尐↘猪︶ㄣ 提交于 2019-12-24 08:24:04

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/3221378/android-how-to-find-out-location-provider-name-from-key-status-changed-broadcas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!