setPackage 用于姜饼中的意图

发布于 2025-01-08 01:08:08 字数 324 浏览 0 评论 0原文

根据 android 文档

或者,从 ICE_CREAM_SANDWICH 开始,您还可以使用 Intent.setPackage 安全地将广播限制到单个应用程序

Gingerbread 中是否有任何方法(可能使用兼容性库)来限制 sendBroadcat() 事件,使其仅将其发送到指定的应用程序包裹?

According to the android documentation:

Alternatively, starting with ICE_CREAM_SANDWICH, you can also safely restrict the broadcast to a single application with Intent.setPackage

Is there any way in Gingerbread (using the compatibility library perhaps) to restrict a sendBroadcat() event such that it only sends it to a specified package?

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

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

发布评论

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

评论(3

痴情换悲伤 2025-01-15 01:08:08

我的第一个建议是使用 LocalBroadcastManager 如果一切皆有可能。这使您可以完全忽略任何安全问题。

如果您确实需要将广播从一个应用程序发送到另一个应用程序,则在 ICS 之前,registerReceiver() 确实不遵守 setPackage 限制,因此在此之前您不能依赖它。做你想做的事并没有什么秘密技巧,只是平台没有这样做的设施。

也就是说......如果您要指定显式的包名称,为什么不直接使用 Intent.setComponent()

另请记住,即使 setPackage() 或 setComponent() 也不会自动完全安全 - 您仍然假设您知道谁在实现该包名称,并且完全有可能使用与您期望不同的应用程序即使您在 Play 商店中拥有该名称,也可以通过侧面加载进行安装。

My first suggestion would be to use LocalBroadcastManager if at all possible. This allows you to completely ignore any security issues.

If you really do need to send the broadcast from one app to another, it is indeed true that registerReceiver() did not respect the setPackage restriction until ICS so you can not rely on it until then. There is no secret trick to do what you want, it's just that the platform doesn't have the facility for it.

That said... if you are to the point of specifying an explicit package name, why not just go all the way and use Intent.setComponent()?

Also keep in mind that even setPackage() or setComponent() are not automatically completely secure -- you are still making an assumption that you know who is implementing that package name, and it is entirely possible for a different app than what you expect to be installed through side-loading, even if you own the name in the Play Store.

权谋诡计 2025-01-15 01:08:08

文档说 setPackage 是在 API Level 4 中引入的,但也许框架发生了变化,使其在 ICS 中的功能不同/更好。创建您自己的接收者能够识别的 Intent Filter 怎么样? 此页面底部附近的记事本示例显示了一个示例:在清单中,NoteEditor 示例指定

这是应用程序定义的自定义操作。

The docs say setPackage was introduced in API Level 4, but perhaps there was a framework change that makes it function different/better in ICS. What about creating your own Intent Filter that your receivers will recognize? The NotePad example near the bottom of this page shows an example: in the manifest, the NoteEditor example specifies

<action android:name="com.android.notepad.action.EDIT_NOTE" />

which is a custom action defined by the app.

情释 2025-01-15 01:08:08

由于广播是由系统处理的,我无法想象有什么方法可以在不接触系统代码的情况下在解决方法中进行编码(因此兼容性包无济于事)。

如果您确实对确保广播安全感兴趣,则可以使用权限遵循 Android 4.0 之前版本的 Android 文档建议:

要在发送时强制执行权限,您可以向 sendBroadcast(Intent, String)sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int、字符串、捆绑)。只有被授予此权限的接收者(通过使用 AndroidManifest.xml 中的标签请求)才能接收广播。

As broadcasts are handled by the system, I cannot imagine that there's any way to code in a workaround without touching the system's code (so the compatibility package won't help).

If you're really interested in keeping a broadcast secure, you can follow the Android docs suggestion for pre-Android 4.0 by using permissions:

To enforce a permission when sending, you supply a non-null permission argument to sendBroadcast(Intent, String) or sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle). Only receivers who have been granted this permission (by requesting it with the tag in their AndroidManifest.xml) will be able to receive the broadcast.

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