[英]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.