【问题标题】:Broadcast Receiver not receiving intent from another app in android 11广播接收器未从android 11中的另一个应用程序接收意图
【发布时间】:2021-02-21 20:07:31
【问题描述】:

我正在尝试在 Android 11 上将广播从应用 A 发送到应用 B。

这是接收器应用 B:
清单:

<receiver android:name="com.example.my_test.TestReceiver"
    android:enabled="true"
    android:permission="com.example.my_test.broadcast_permission">
    <intent-filter>
        <action android:name="com.example.my_test.receive_action"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

接收器类:

class TestReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("MY_TAG", "received: ${intent?.getIntExtra("data", 0)}")
    }
}

这是发件人应用 A:
清单:

<uses-permission android:name="com.example.my_test.broadcast_permission"/>
...
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
...

发件人代码(MainActivity 内):

findViewById<Button>(R.id.button).setOnClickListener {
    val intent = Intent("com.example.my_test.receive_action")
    intent.addCategory("android.intent.category.DEFAULT")
    intent.component = ComponentName("com.example.my_test", "com.example.my_test.TestReceiver")
    intent.putExtra("data", 69)
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
    sendBroadcast(intent, "com.example.my_test.broadcast_permission")
}

这是我迄今为止所尝试的一切。也不确定这里是否有任何关于广播许可的错误。没有任何效果,TestReceiver 类从不记录任何内容。

我也试过android:exported="true"

如果有人知道我在哪里犯了错误,请提供帮助。如果不可能,有没有其他方法可以将数据从一个应用程序传递到另一个应用程序?谢谢。

【问题讨论】:

    标签: android broadcastreceiver android-broadcast android-11


    【解决方案1】:

    解决了。以下是几点:

    1. 在应用程序 B(接收方)中,还需要在清单顶部的 &lt;permission&gt; 标记中声明权限。我错过了这个。
    2. Category 不是必需的。也无需在sendBroadcast() 中声明权限
    3. App A(发件人)中的&lt;uses-permission&gt; 是必需的。
    4. 在 Android 11 中需要提及 ComponentName 或包名称(使用 setPackage())。

    这是更正后的代码:

    这里是接收器应用 B:
    清单:

    <permission android:name="com.example.my_test.broadcast_permission"/>
    ...
        <application>
        ...
            <receiver android:name="com.example.my_test.TestReceiver"
                android:permission="com.example.my_test.broadcast_permission">
                <intent-filter>
                    <action android:name="com.example.my_test.receive_action"/>
                </intent-filter>
            </receiver>
        ...
        </application>
    ...
    

    接收者类别:没有变化。

    这里是发件人应用 A:

    清单:没有变化

    发件人代码(MainActivity 内):

    findViewById<Button>(R.id.button).setOnClickListener {
        val intent = Intent("com.example.my_test.receive_action")
        intent.component = ComponentName("com.example.my_test", "com.example.my_test.TestReceiver")
        // if exact receiver class name is unknown, you can use:
        // intent.setPackage("com.example.my_test")
        intent.putExtra("data", 69)
        sendBroadcast(intent)
    }
    

    【讨论】:

    • 嗨,我似乎找不到#4 的来源。我感兴趣的是它是否只影响针对 Android 11 的应用程序,或者它是否像 Android 11 上的那样,无论目标 SDK 是什么。另外,如果是前者,在Android 10或更低的设备上运行应用程序时是否也需要setPackage()?
    • 这个答案来自我的经验。我还没有找到任何来源。无论如何,从 Android 10 及以下,您可以在没有任何清单权限的情况下查看所有包。那为什么不加上receiver的包名呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多