【发布时间】:2019-11-27 12:44:43
【问题描述】:
我在火车上,突然想到一件事。
如果允许我如下声明我的服务:
<service
android:name=".service.MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="INTENT_SAMPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</service>
我知道执行以下操作会导致IllegalArgumentException: Service Intent must be explicit 错误。
Intent intent = new Intent("INTENT_SAMPLE");
intent.setType("text/plain");
startService(intent);
那么,这种情况下intent-filter有什么用,如果有的话?
【问题讨论】:
-
只有当您希望第三方应用程序与该组件通信时,该组件上才具有
。您似乎陷入了假设所有事情都需要一个 的陷阱——实际上,您很少需要一个 。显式 Intent 是指在 Intent 本身中指定要与之对话的组件,通常使用将 Java Class 对象作为第二个参数的构造函数。那,而不是隐式 Intent 和 ,应该用于应用程序本地的组件 -
曾经有过。在 Android 的历史上,这个例外相对较新。安全问题迫使 Android 设计者通过手术移除原始功能。您正在查看手术留下的疤痕。
标签: android intentfilter