android - 为什么 PendingIntent 不发回我的 Intent 自定义 Extras 设置?

标签 android android-intent extras

这个问题在某种程度上与我寻找 get the extras back in startActivityForResult 时的问题有关。但现在我面临另一个挑战。

我已订阅接收 ProximityAlerts,并且我已明确构建 Intent 以包含一些 Extras。但是当我得到服务时,额外的东西不在那里。

这里的答案是工作代码:

Intent intent = new Intent(this, PlacesProximityHandlerService.class);
intent.setAction("PlacesProximityHandlerService");
intent.putExtra("lat", objPlace.getLat());
intent.putExtra("lon", objPlace.getLon());
intent.putExtra("error_m", objPlace.getError()+ALERT_RANGE_IN_METERS);
PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);
LocationUtils.addProximity(this, objPlace.getLat(), objPlace.getLon(),objPlace.getError()+ALERT_RANGE_IN_METERS, -1, sender);

文档说 param PendingIntent to be sent for each location update

最佳答案

由于某些未指定的原因,仅当您设置了某些操作(例如 setAction("foo"))时才会提供附加功能。 CommonsWare 所指的内容仅适用于获取 PendingIntent 实例时,如果您尚未设置 FLAG_ONE_SHOT。这可以通过 PendingIntent.get... 工厂方法中的 requestCode 参数来解决。虽然文档说它目前没有使用,但实际上在区分 PendingIntents 时会考虑在内。

在您的情况下,除了一些虚拟操作字符串之外,您不需要设置任何其他内容。 LocationManagerService 重用您订阅的接近警报的 PendingIntent,并且仅在手机进入或退出警报范围时添加标志。

关于android - 为什么 PendingIntent 不发回我的 Intent 自定义 Extras 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127957/

相关文章:

java - Android:如果 Activity 已经在运行,如何捕获 Intent 的 Extras?

android - 强制平板电脑使用 xhdpi 资源

Android 布局 - ScrollView 内的 ImageView - 图像前的空间

android - 如何从通知中触发广播接收器?

android - 如何通过 Intent 将数据从小部件配置类传递到小部件提供程序类?

Android 不断缓存我的 Intents Extras,如何声明一个未决的 Intent 来保留新的 Extras?

android - 软键盘在 Android 12 上的 flutter WebView 中停止工作

android - 使用 WSL2 为空的 ADB 设备列表

android - 第二次单击时,TabActivity 中的 Activity 不运行 onCreate 方法

android - 将对象作为下一个 Activity 的 Intent