Android 中服务接收器和广播接收器之间的区别
我想知道服务和广播接收器之间的区别,任何人都可以指出一个可以在 Android 移动设备上观察到的示例。 谢谢
I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务:如果您想在后台执行某些操作,即使应用程序关闭,它也将始终在后台运行。您可以在单独的过程中创建它,如果需要,您也可以将服务提供给其他应用程序。下载任何内容或音乐是广播接收器的一个很好的例子
:通常系统会发送一些信息,如果您愿意,可以通过注册来接收这些信息。当事情发生时,你可以使用 onReceive 方法做你想做的事情。
例如,当新短信到达或启动完成时,系统将发送 BroadcastReceiver
这是一篇好文章: 服务和广播接收器
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method.
Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Here is good article : Service and BroadcastReceiver
Service
用于当您想要在后台执行某些操作时,任何长时间运行的进程都可以使用后台服务来完成。例如,您想在应用程序关闭时播放音乐。在这种情况下,服务将在后台运行并伴有音乐。服务示例
BroadcastReceiver
当您想在某些事件期间触发某些内容或代码时使用。例如,事件可以发生在设备启动时。如果您想在设备启动、日期和时间更改等时执行某些操作...BroadcastReceiver 示例
Service
is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.Example of Service
BroadcastReceiver
is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc...Example of BroadcastReceiver
我认为可能有不同的方式。 服务接收专门发送到您的应用程序的意图,就像活动一样。 广播接收器接收在系统范围内向设备上安装的所有应用程序广播的意图。
(我说服务有点像活动的原因是:您不会在设备上安装的所有应用程序中广播一条消息说“启动 Activity
MyActivity
”。它仅适用于您的特定当然,正如其他人提到的,服务可以继续在后台运行,而广播接收器应该快速完成(例如,如果它运行超过 5 秒,它可能会被操作系统杀死)。在某些情况下,广播接收器仍然可以在后台运行(当应用程序关闭时)。为此,值得一提的是,实际上有两种类型的广播接收器 - 清单声明的和上下文注册的。它们有不同的生命周期和限制——前者可以在后台接收广播,但有一定的限制,而后者不能在后台接收广播(应用程序必须正在运行且处于活动状态),但对可以接收的意图类型没有限制。
服务和广播接收器都必须被专门调用(通过意图),但是对于服务来说,这通常是一个特定的调用(例如,当您的应用程序启动时或当用户单击某个按钮时),而对于广播接收器,它们不需要显式启动,因为当进行相关广播时它们无论如何都会启动。
我是这样想的:
1:仅如果您的目标系统是 Android 8.0 或更高版本。如果意图专门针对您的应用程序,则不应用这些限制。可以在此处找到受限制的意图列表。
I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity
MyActivity
" across all apps installed on the device. It is only for your specific app.)Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.
Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.
Here's how I would think of it:
1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.