Android:如何从 LocationManager 发送的 KEY_STATUS_CHANGED 广播意图中查找位置提供商名称?
我尝试同时与网络和 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);
}
在我的广播接收器(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";
}
}
问题是:如何确定来自哪个提供者到达此意图? (String provider_name = ???;)
我尝试在注册时将意图提供者名称作为有效负载包含在内,如下所示:
intent.putExtra("local.home.gpstest.PROVIDER", provider);
但不成功:来自提供者的传入意图删除了我的数据...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案。这是对 Android 如何使用 PendingIntents 的误解。事实上,具有额外负载的解决方案正在工作,但如果之前已经对没有负载数据的 PendingIntents 发出了一些请求,则应用程序会收到没有负载数据的旧意图。有必要重新加载设备或对带有标志的 PendingIntent 发出新请求:
之后传入的意图将携带有关提供者名称的信息。
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:
After that incoming intent will carry information about provider name.