对于广播接收器,类别“android.intent.category.DEFAULT”有什么用?
<receiver
android:name=".receivers.InputReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.amazon.intent.action.CUSTOM_A" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Android documentation表示要匹配任何隐式 Intent ,请添加类别默认值。
Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.
在 Android-O 之后,我将无法发送隐式广播。这是否意味着“CATEGORY_DEFAULT”对广播没有用?
我对在广播和 Activity 中使用 CATEGORY_DEFAULT 感到很困惑。有人可以澄清我吗?使用 CATEGORY_DEFAULT 的正确场景是什么?如果我不添加它会发生什么?
最佳答案
What is the right scenario to use CATEGORY_DEFAULT
当您调用 startActivity()
时或 startActivityForResult()
, 如果 Intent
其中没有类别,框架将添加 android.intent.category.DEFAULT
到 Intent
, 在尝试识别应该响应 Intent
的组件之前.这就是为什么 <intent-filter>
对于 <activity>
通常有一个 <category>
元素,要么是android.intent.category.DEFAULT
或其他类别。
当您调用 startService()
时, bindService()
, 或 sendBroadcast()
, 如果 Intent
里面没有类别,框架留下 Intent
独自的。这就是为什么 <intent-filter>
对于 <service>
或 <receiver>
经常缺少 <category>
元素,因为不需要类别。
and what would happen if I didn't add it?
如果 Intent
包含一个类别,而您的 <intent-filter>
不包含类别,您的 <intent-filter>
将不匹配 Intent
.这就是为什么我们通常需要 <category>
与 <activity>
, 自 Intent
可能有一个类别。
在com.amazon.intent.action.CUSTOM_A
的情况下来自你的 <receiver>
在您的代码 fragment 中,显然亚马逊选择添加 android.intent.category.DEFAULT
到他们的广播Intent
手动。这很奇怪,但 SDK 允许这样做。
关于android - 类别 DEFAULT 与广播接收器有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345221/