setPackage 用于姜饼中的意图
根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的第一个建议是使用 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.
文档说
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 specifieswhich is a custom action defined by the app.
由于广播是由系统处理的,我无法想象有什么方法可以在不接触系统代码的情况下在解决方法中进行编码(因此兼容性包无济于事)。
如果您确实对确保广播安全感兴趣,则可以使用权限遵循 Android 4.0 之前版本的 Android 文档建议:
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: