Android 中的内部广播

发布于 2024-12-12 12:41:14 字数 158 浏览 4 评论 0原文

是否可以发起具有应用范围的广播?那就是:在应用程序之外无法检测到广播?

我使用广播将一些事件传达给用户界面。但最近我为另一个应用程序回收了一些代码,我发现广播正在唤醒两个应用程序(正如预期的那样)。所以我想知道除了改变Intent的“action”值之外是否还有其他方法可以解决这个问题

Is possible to launch broadcast with application scope? That is: broadcasts undetectable out of the application?

I was using broadcast for communicate some events to the UI. But recently I recycled some of my code for another app and I found that the broadcasts were awakening both applications (as it was expected). So I wonder if there is any way around this besides changing the value of "action" of Intent

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

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

发布评论

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

评论(6

瑾兮 2024-12-19 12:41:14

如果您希望在应用程序内传递消息,您应该使用 LocalBroadcastManager,它是普通广播的最佳替代方案。阅读这篇文章以了解实施:如何使用 LocalBroadcastManager?

You should use LocalBroadcastManager, its best alternative to normal broadcast, if you want intra application message passing. Read this post for implementation: how to use LocalBroadcastManager?.

缺⑴份安定 2024-12-19 12:41:14

为什么不使用 观察者-可观察模式在应用程序中的 java 类之间进行通信?

Why do not you use observer-observable pattern to communicate among java classes in your application ?

十秒萌定你 2024-12-19 12:41:14

是的,您可以通过 使用组件名称Intent.setComponent()

但如果您的广播始终处于进行中状态,则无需使用它,不是吗?您可以只使用普通的旧函数调用...

Yes, you can use the component name via Intent.setComponent().

But if your broadcast is always going to be in-process, there's no need to use it, is there? You can just use a plain old function call...

因为看清所以看轻 2024-12-19 12:41:14

我认为你必须改变你的操作,如果你使用 setComponent 是明确的意图,那么如果这对你来说没问题的话,它会起作用,但是对于隐式意图,你不能通过应用程序进行限制

有cyanogenmod,你可以在其中签署你的应用程序,然后你可以防止在您的手机中安装具有不同签名的应用程序,这意味着您在手机上安装某些内容的人将无法安装新的应用程序(除了您),这可以确保没有人会使用与您相同的操作名称注册接收

器:
在应用程序中使用广播进行内部通信是错误的,并且比正常调用过程慢(这在您的情况下是可能的)。我不认为您有什么理由不应该拨打普通电话。

I think you will have to change your action, If you use setComponent that's explicit intent, then if that is fine for you it will work, but for implicit intent you can not restrict by application

There is cyanogenmod where you can sign your application and you can prevent instaling apps in you phone with different signature, which means ones you install something on phone no one will be able to install new app(except you) and this can restasure that no one will register receiver with the same action name as you

Note:
using broadcast for inter communication in the app is wrong and slower than normal call to procedure (which is possible in your case). I do not see a reason why you shouldn't make a normal call.

长安忆 2024-12-19 12:41:14

@Lukap:签名应用程序是所有 Android 版本中的一项基本功能,仅允许授权方为已安装的应用程序提供更新/更改。

开发指南

所有申请都必须签名。如果未签名,系统不会在模拟器或设备上安装应用程序。

加:

注意:在应用程序中使用广播进行相互通信是错误的

这个是错误的。在应用程序内部使用广播的原因有很多,在某些情况下,如果没有它,您确实无法生存。

@Lukap: Signed applications is an essential feature in all Android versions to allow only authorized parties to provide updates/changes for an installed app.

Dev Guide:

All applications must be signed. The system will not install an application on an emulator or a device if it is not signed.

Plus:

Note: using broadcast for inter communication in the app is wrong

This is wrong. There are a number of reasons to use broadcast inside an application, and some situations where you really cannot live without it.

沫尐诺 2024-12-19 12:41:14

使用 LocalBroadcastManager。您将需要包含支持库,但这是解决此问题的一个很好的解决方案,因为它比通过系统发送全局广播更有效。

Use LocalBroadcastManager. You will need to include the support library, but this is a great solution for solving this issue as it is more efficient than sending a global broadcast through the system.

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