我尝试同时使用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";
}
}
问题是:如何确定从哪个提供商到达此意图? (String provider_name = ???;)
我尝试在注册时刻将意图提供者名称包含在有效负载中,如下所示:
intent.putExtra("local.home.gpstest.PROVIDER", provider);
但未成功:来自提供商的传入意图会删除我的数据......
答案 0 :(得分:1)
我找到了解决方案。这是一个误解Android如何与PendingIntents合作的误解。实际上,有效负载额外的解决方案正在发挥作用,但如果之前已经对没有有效载荷数据的PendingIntents做出了一些请求,则应用程序会接收没有有效载荷数据的旧意图。有必要重新加载设备或使用标志对PendingIntent发出新请求:
PendingIntent pi = PendingIntent.getBroadcast(GpsMain.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
之后,传入的意图将包含有关提供者名称的信息。