问题描述
当我们在清单中声明这样的内容时,是否可以保证在接收到该类型的意图时将执行该服务(如果尚未运行)?在Android Oreo 8.0上是否可以以相同的方式工作?
When we declare something like this in the manifest, is it guaranteed that the service will be executed (if it is not already running) on receiving an intent of that type? Does this work the same way on Android Oreo 8.0?
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
推荐答案
保证"是一个强有力的术语.通常您的说法是正确的.不会出现的情况:
"Guaranteed" is a strong term. Generally your statement is true. Scenarios where it will not be include:
-
如果有人尝试使用隐式的
Intent
启动服务,则仅包含该操作字符串.设备很可能会使用< intent-filter>
来提供多种服务,在这种情况下,Android只会选择其中一项服务来启动.请注意,Android 5.0+禁止使用隐式Intent
进行 binding ,以避免这种情况.
If somebody tries starting the service using an implicit
Intent
, one with just that action string. Most likely a device will have several services with that<intent-filter>
, in which case Android will just pick one of those services to start. Note that binding using an implicitIntent
is banned on Android 5.0+, to avoid this sort of situation.
如果在运行时使用 PackageManager
和 setComponentEnabledSetting()
禁用了组件.
If you disabled the component at runtime using PackageManager
and setComponentEnabledSetting()
.
我不知道Android 8.0中< service>
上< intent-filter>
的行为有任何变化.Android 8.0中的服务有所更改(例如,无法从后台启动服务),但是这些更改不依赖于< intent-filter>
.
I am not aware of any changes in the behavior of <intent-filter>
on <service>
in Android 8.0. There are changes to services in Android 8.0 (e.g., cannot start a service from the background), but those changes do not depend on the existence of an <intent-filter>
.
这篇关于Android服务和Intent过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!