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

发布于 2024-09-07 04:40:05 字数 676 浏览 7 评论 0原文

这个问题在某种程度上与我寻找

我已订阅接收 ProximityAlerts,并且已明确构建意图以包含一些额外内容。但当我获得服务时,附加服务不存在。

在答案之后是工作代码:

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);

文档说参数为每个位置更新发送的PendingIntent

This questions somehow relates to the question when I was looking to get the extras back in startActivityForResult but now I face another challenge.

I have subscribed to receive ProximityAlerts and I have explicitly constructed the Intent to include some Extras. But when I got the service the extras are not there.

After the answers here is the working code:

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);

The documentation says param PendingIntent to be sent for each location update

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹂绝世的画 2024-09-14 04:40:05

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

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

For some unspecified reason, extras will be delivered only if you've set some action, for example setAction("foo"). What CommonsWare refers to applies only when obtaining PendingIntent instances, if you haven't set FLAG_ONE_SHOT. That can be fixed by the requestCode argument in PendingIntent.get... factory methods. Although documentation says it's currently not used, it actually takes into count when distinguishing PendingIntents.

In your case, you don't need to set anything else than some dummy action string. LocationManagerService reuses the PendingIntent you have subscribed for proximity alerts, and only adds a flag if phone has entered or exited the alarm range.

南汐寒笙箫 2024-09-14 04:40:05

如果您有多个未完成的 PendingIntents,则需要确保基础 Intents 的差异不仅仅是其额外内容。否则,Android 将继续重用您为第一个 Intent 创建的第一个 PendingIntent,并始终使用第一个 Intent 的额外内容。

例如,您可以通过 setAction() 添加一个独特的操作——这不会改变您的 Intent 路由(因为您正在指定组件),但它会使您的意图不同。

If you have multiple outstanding PendingIntents, you need to make sure that the underlying Intents differ on more than their extras. Otherwise, Android will keep reusing the first PendingIntent you created for your first Intent, using that first Intent's extras all of the time.

For example, you could add a unique action via setAction() -- that will not change your Intent routing (since you are specifying the component), but it will make your Intents different.

浪菊怪哟 2024-09-14 04:40:05

我遇到了这个问题,我发现的解决方案非常简单,尽管我无法解释它为什么有效。

最初,我的待处理意图如下所示:

     notificationIntent = new Intent(ctx, FragmentTabsPager.class);
     notificationIntent.setData(Uri.parse("content://com.sbs.mobile.workorder.WorkOrder/notes/"));
     notificationIntent.putExtra("NOTIFICATION", true);   
     notificationIntent.putExtra(WorkOrder.WorkOrderColumns.WORKORDERID, submessage);

当创建这样的意图时,单击通知时不会传递额外内容,接收活动中的额外映射将为空。我对初始化 notificationIntent 的行进行了以下更改:

     notificationIntent = new Intent().setClass(ctx, FragmentTabsPager.class);

现在,额外内容已填充到接收活动中。再说一遍,我无法解释为什么它有效,但它解决了我的问题。

I had this problem and the solution I found was quite simple, though I can't explain why it worked.

Initially my pending intent looked like this:

     notificationIntent = new Intent(ctx, FragmentTabsPager.class);
     notificationIntent.setData(Uri.parse("content://com.sbs.mobile.workorder.WorkOrder/notes/"));
     notificationIntent.putExtra("NOTIFICATION", true);   
     notificationIntent.putExtra(WorkOrder.WorkOrderColumns.WORKORDERID, submessage);

When creating the intent like this, no extras would be passed when the notification was clicked, the extras map would be empty in the receiving activity. I made the following change to the line initializing the notificationIntent:

     notificationIntent = new Intent().setClass(ctx, FragmentTabsPager.class);

Now the extras are populated in the receiving activity. Again, I can't explain why this works but it fixed my problem.

荒人说梦 2024-09-14 04:40:05

没有一个答案对我有用。将操作设置为特定字符串第一次有效,但如果稍后使用具有不同附加功能的相同通知,则它将不起作用。我用随机生成的字符串替换了 setAction 方法的字符串,并且它的工作没有任何问题:

intent.setAction(new Random().nextInt(50) + "_action");
  • 如果您认为您可能会经常使用通知(例如下载不同的文件),则传递更大的数字到 nextInt()

None of the answers worked for me. Setting action to a specific string works for the first time but if you use the same notification with different extras at a later time, it would not work. I replaced the string for the setAction method with a randomly generated one and it works without any issues:

intent.setAction(new Random().nextInt(50) + "_action");
  • If you think that you might use the notification a lot (Like for downloading different files) then pass a larger number to nextInt()
蓬勃野心 2024-09-14 04:40:05

关键是在调用之前之前将额外内容和唯一操作设置到意图中,

PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);

如果在调用上述内容之后之后将额外内容和操作设置到意图中,则不会起作用。
这将不起作用

Intent intent;  
PendingIntent sender=PendingIntent.getService(this, 0,   
   intent=new Intent(this, PlacesProximityHandlerService.class), 0);  

intent.setAction("PlacesProximityHandlerService");  
intent.putExtra("lat", objPlace.getLat());  

The key is to set the extras and the unique action into the intent before calling

PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);

if you set the extras and action into the intent after calling the above, it won't work.
This will not work:

Intent intent;  
PendingIntent sender=PendingIntent.getService(this, 0,   
   intent=new Intent(this, PlacesProximityHandlerService.class), 0);  

intent.setAction("PlacesProximityHandlerService");  
intent.putExtra("lat", objPlace.getLat());  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文