安卓:隐性意图与广播接收机接收机、隐性、意图

2023-09-07 22:45:16 作者:伱是涐今生仅有旳幸福

我来加快速度Android开发和隐含的意图和广播接收器之间的区别还不清楚。我是在区分这些概念,希望帮助以及何时使用两个。

I'm coming up to speed on Android development and the distinction between an implicit intent and a broadcast receiver is unclear. I was hoping for help in distinguishing these concepts and when to use the two.

都接收意图,既反应系统消息,那么为什么是广播接收机甚至需要并且是当它用,而不是一个隐含的意图和意图过滤接受隐含的意图是什么?

Both receive intents, both react to system messages, so why is a broadcast receiver even needed and when is it used as opposed to an implicit intent and intent filter to accept the implicit intent?

推荐答案

广播是 - 信息广播给任何人听。他们本质上是不安全和交付到预期收件人不能保证,因为实在是没有一个预期的收件人。例如, CONNECTIVITY_CHANGE 广播使这很清楚:在Android设备连接的变化,许多应用程序可能会感兴趣。而不是 ConnectivityManager 不必通知通过特定的每个应用程序意图,它发送一个广播。已注册本次活动感兴趣的任何应用程序将另行通知。任何应用程序不运行或不关心......不会的。

Broadcasts are just that -- messages broadcast to anyone listening. They are inherently insecure, and delivery to the intended recipient isn't guaranteed, because there really isn't an intended recipient. For example, the CONNECTIVITY_CHANGE broadcast makes this quite clear: When connectivity changes in an Android device, many apps might be interested. Rather than the ConnectivityManager having to notify each app via specific Intent, it sends a broadcast. Any app that has registered interest in this event will be notified. Any app that isn't running or doesn't care... won't.

这是意图被派,当一个应用程序或活动要发动另一做的很具体的事情。例如,文件管理器可能需要启动一个图像浏览器或视频播放器。您的应用程序可能要启动一个特定的活动在您的应用程序的另一个等特定意图的通信(即包括包名称和组件名称),不易被截取的,所以它有点更安全。最重要的是,这里只有且只有一个接收器 - 如果没有,可以发现,在意图将失败

An Intent is "sent" when one app or Activity wants to launch another to do something very specific. For example, a file-manager might want to launch an image viewer or video player. Your app might want to launch a very specific Activity within another one of your apps, etc. The communication by specific intents (i.e. including package name and component name) can not easily be intercepted, so it's somewhat more secure. Most importantly, there's only and exactly one "receiver" -- if none can be found, the Intent will fail.

此外, BroacastReceiver 将一个活动中活跃服务和接收到的广播通常只改变状态和/或做轻微的UI更新...例如,如果你的互联网连接被删除,你可能会禁用一些操作。相比之下,一个特定意图通常会推出一个新的活动或将现有的前台。

Further, a BroacastReceiver will be active within an Activity or Service and received broadcasts will generally only change state and/or do minor UI updates... for example, you might disable a few actions if your internet connectivity is dropped. By comparison, a specific Intent will usually launch a new Activity or bring an existing one to the foreground.