android - 将广播从 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 发送的广播吗?有什么可以阻止广播接收器的吗?
解决方案
推荐阅读
- ggplot2 - 在ggplot中绘制极小的Y轴值
- c++ - 我不明白如何计算素数并且 modPow 函数在以下代码中工作
- c# - Dynamics 365 和网站之间的连接
- splunk - 如何在 splunk 查询中区分模式 /bank/*/accounts/ 与 /bank/4/accounts/a1 的消息?
- python - 将所有 pcap 文件转换为带有所需列 python 的 csv
- java - 使用java从CSV读取列数据中的多行
- cross-platform - MSYS2 上的 Perl6 脚本导致“无法统计文件”错误
- python-3.x - Python脚本从文本文件grep字段并将输出写入csv文件
- r - 在 df 的两个变量中找到第一次出现
- typo3 - 如何为 TYPO3 9.5 返工 TYPO3 8 AbstractViewHelper