android - 类别 DEFAULT 与广播接收器有什么用?

标签 android broadcastreceiver categories android-broadcast

对于广播接收器,类别“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.DEFAULTIntent , 在尝试识别应该响应 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/

相关文章:

android - 带有导航组件的抽屉布局 - 仅监听一项

java - 如何使用 retrofit2 接收 Json 数组 ("result")

android - 停止短信移动到收件箱

android - 未收到来自 Chrome 自定义选项卡菜单项的广播点击

android - BroadcastReceiver 中的 ApplicationContext

Objective-C : adding attribute to a category

android - 从哪里开始 : Creating a PDF in my Android application

objective-c - Objective C 类别的实例变量

mysql - Catalog_eav_attribute 中没有与我的 'created_at' 属性 ID 相关的行

java - 带有警报管理器的 Android 上下文