首页 > 解决方案 > 将广播从 App A 发送到 App B 并返回

问题描述

制作了两个简单的应用程序 A 有一个活动和一个广播接收器。App B 只有一个广播接收器。

我正在从 A 向 B 发送广播。然后我从 B 向 A 发送响应。

A -> B -> A

所以这项工作非常好。然后我尝试在我的实际应用程序中集成应用程序 A 中的相同内容以与 B 进行通信。突然,B 的响应不起作用。

Actual app -> B -> Nothing

我的实际应用程序中的广播接收器似乎无法注册。实际应用清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.app.customer">

    <queries>
        <package android:name="com.app.b" />
    </queries>

    <application
        android:name=".App"
        android:allowBackup="false"
        android:appCategory="productivity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:resizeableActivity="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/appTheme.DayNight"
        tools:targetApi="o">

        <activity
            android:name=".home.SomeActivity"
            android:clearTaskOnLaunch="true"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" /> 

        <receiver
            android:name="com.app.a.SomeReceiver"
            android:enabled="true"
            android:exported="true"
            tools:ignore="ExportedReceiver">
            <intent-filter>
                <action android:name="com.app.a.RESPONSE" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

实际应用广播接收器:

package com.app.a

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

class SomeReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        // Nothing is ever received here
    }
}

从应用 B 制作的广播:

override fun onReceive(context: Context, receiverIntent: Intent) {
    val intent = Intent()
    intent.action = "com.app.a.RESPONSE"
    intent.putExtra("message", "Hello")
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
    intent.component = ComponentName("com.app.a", "com.app.a.SomeReceiver")
    context.sendBroadcast(intent)
}

知道为什么 SomeReceiver 没有接收到 App B 发送的广播吗?有什么可以阻止广播接收器的吗?

标签: android

解决方案


推荐阅读