是否可以多次触发同一个意图?

发布于 2024-12-05 21:35:07 字数 618 浏览 0 评论 0原文

我有一项进行数据库调用的服务。该服务接收带有意图的请求,当数据库调用完成时,它会广播一个“更新完成”意图,指示调用完成。

有时数据库已经填充了缓存数据,在这种情况下,我想立即广播“更新完成”意图,指示活动应该显示缓存数据,然后一旦数据库更新,就会触发另一个“更新完成”意图指示活动应该加载更新的数据。

问题是该活动永远不会收到第二个广播。这是因为我正在重复使用已被触发的相同意图对象吗?

这是代码:

if (scheduleDatabase.populated()) {
    intent.putExtra("fromCache", true);
    getApplicationContext().sendBroadcast(intent);
}

scheduleDatabase.update();
intent.putExtra("fromCache", false);
getApplicationContext().sendBroadcast(intent);

更新: 如果我注释掉其中一个意图广播,则另一个总是会触发并被接收。 另外,如果我使用相同的操作字符串创建两个意图对象并分别触发它们,则活动只会接收第一个意图对象。我还不清楚另一个是否被解雇但没有收到,或者是否根本没有被解雇。

I have a service that does database calls. The service receives a request with an intent, and when the database call is complete, it broadcasts an "update complete" intent indicating the completion of the call.

Sometimes the database is already populated with cached data, in which case I would like to immediately broadcast an "update complete" intent, indicating the activity should display the cached data, and then once the database has been updated fire another "update complete" intent indicating the activity should load the updated data.

The problem is that the second broadcast is never received by the activity. Is this because I'm re-using the same intent object that has already been fired?

Here's the code:

if (scheduleDatabase.populated()) {
    intent.putExtra("fromCache", true);
    getApplicationContext().sendBroadcast(intent);
}

scheduleDatabase.update();
intent.putExtra("fromCache", false);
getApplicationContext().sendBroadcast(intent);

An update:
If I comment out one of the intent broadcasts, the other one always fires and is received.
Also, if I create two intent objects with the same action string and fire them separately, only the first one is ever received by the activity. I'm not clear yet on whether the other gets fired but not received, or if it never fires at all.

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

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

发布评论

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

评论(2

安稳善良 2024-12-12 21:35:07

事实证明,我在意图方面遇到的问题是我的代码不同区域中更大问题的症状。解决该问题后,意图开始按照我的预期发射和接收。因此,为了回答我自己最初的问题,是的,可以多次触发同一个意图。

It turns out that the problems I had with the intents were a symptom of a larger problem in a different area of my code. After fixing that problem, the intents began firing and receiving as I expected. So, to answer my own original question, yes it is possible to fire a single intent multiple times.

煮酒 2024-12-12 21:35:07

我遇到了类似的问题。是的,第二个意图是覆盖第一个意图。我的解决方法是在我的 BroadcastReceiver 中创建一个新的 Intent。您可以访问相同的上下文,并且可以在附加项中传递数据。

I ran into a similar problem. Yes the second intent is overwriting the first one. My workaround was to create a new Intent in my BroadcastReceiver. You have access to the same context, and can pass data in the extras.

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