这个问题在某种程度上与我寻找 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/