Dou*_*ghy 20 android geolocation intentfilter android-intent
我正在尝试使用Android的LocationManager requestLocationUpdates.一切正常,直到我尝试提取广播接收器中的实际位置对象.我是否需要专门为我的自定义意图定义"额外内容",以便在我将其传递给requestLocationUpdates之前安装Android LocationManager,以便知道如何将其添加到intent中,或者它是否会创建extras-bundle,无论它何时通过触发意图广播接收器?
我的代码看起来像这样:
Intent intent = new Intent("com.myapp.swarm.LOCATION_READY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//Register for broadcast intents
int minTime = 5000;
int minDistance = 0;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, pendingIntent);
Run Code Online (Sandbox Code Playgroud)
我有一个广播接收器,在宣言中定义为:
<receiver android:name=".LocationReceiver">
<intent-filter>
<action android:name="com.myapp.swarm.LOCATION_READY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
广播接收机类如下:
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get("KEY_LOCATION_CHANGED");
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我的"loc"对象即将出现.
Dou*_*ghy 20
好的,我设法通过将广播接收器代码中的KEY_LOCATION_CHANGED更改为:
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
nif*_*ifo 14
我试图编码和测试你提出的解决方案,因为我面临着关于接近警报和携带位置对象的意图的类似问题.根据您提供的信息,您设法克服了BroadcastReceiver方面的空对象检索.您可能没有注意到的是,现在您应该收到与您的意图首次创建时相同的位置(也称为:意图缓存问题).
为了克服这个问题,我使用了FLAG_CANCEL_CURRENT,这是很多人在这里提出的,它工作得非常好,取得了新的(和多汁的:P)位置值.因此,定义待处理意图的行应如下所示:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
但是,如果出现以下情况,可以忽略
| 归档时间: |
|
| 查看次数: |
19096 次 |
| 最近记录: |