Android OrderedBroadcast 在发布版本中不工作
Android OrderedBroadcast Not Working In Release Build
为什么 OrderedBroadcast 在应用程序的调试版本中有效,但在发布中无效?我发送以下 OrderedBroadcast:
context.sendOrderedBroadcast(sendInt, "xxx.xxxx.permission.API", new BroadcastReceiver() {
@SuppressLint("NewApi")
@Override
public void onReceive(Context receivercontext, Intent intent) {
Bundle results = getResultExtras(true);
if (results.getInt("Result", Activity.RESULT_CANCELED) == Activity.RESULT_OK) {
Log.d("DEBUG", "OK");
} else {
Log.e("DEBUG", "Failed");
}
}
}, null, Activity.RESULT_OK, null, null);
两个应用程序在 AndroidManifest.xml 文件中都有适当的权限,接收者声明如下:
<receiver android:name="xxx.xxxx.xxxx.Receiver1"
android:enabled="true"
android:exported="true"
android:permission="xxx.xxxx.permission.API">
<intent-filter>
<action android:name="xxx.xxxx.permission.API.1" />
</intent-filter>
</receiver>
正如我所提到的,如果我在调试版本中同时拥有发送方和接收方应用程序 运行,那么一切都会完美运行,但是如果我 运行 接收方应用程序处于发布模式(没有 proguard 或任何东西)发件人应用程序刚刚获得 RESULT_CANCELLED
结果?
这困扰了我好几天,如有任何想法,我们将不胜感激。
听起来您在声明权限时正在使用 android:protectionLevel="signature"
。如果您 运行 在调试中发送发件人,它会使用您的调试密钥进行签名。接收方将使用发布密钥进行签名。因此,签名将不匹配,接收者将不会被授予许可。
您需要 运行 两者都处于发布模式,以便它们都使用相同的密钥签名。
好的,经过大量搜索和试验后发现这是 Android 的一个相对简单但烦人的安全功能导致了这个错误:
安装的应用程序只有在首次在设备上打开时才能接收广播(普通广播或有序广播)并对其进行操作。在我的例子中,调试版本在 运行 时自动打开,但发布版本没有,并且它没有应用程序图标,仅作为主应用程序的扩展,因此从未打开。
因此,解决方法是为接收应用程序也添加一个应用程序图标,并确保它在设备上 运行。奇怪的是 logcat 中没有生成安全错误,所以除非您知道这种事情,否则很难调试!
为什么 OrderedBroadcast 在应用程序的调试版本中有效,但在发布中无效?我发送以下 OrderedBroadcast:
context.sendOrderedBroadcast(sendInt, "xxx.xxxx.permission.API", new BroadcastReceiver() {
@SuppressLint("NewApi")
@Override
public void onReceive(Context receivercontext, Intent intent) {
Bundle results = getResultExtras(true);
if (results.getInt("Result", Activity.RESULT_CANCELED) == Activity.RESULT_OK) {
Log.d("DEBUG", "OK");
} else {
Log.e("DEBUG", "Failed");
}
}
}, null, Activity.RESULT_OK, null, null);
两个应用程序在 AndroidManifest.xml 文件中都有适当的权限,接收者声明如下:
<receiver android:name="xxx.xxxx.xxxx.Receiver1"
android:enabled="true"
android:exported="true"
android:permission="xxx.xxxx.permission.API">
<intent-filter>
<action android:name="xxx.xxxx.permission.API.1" />
</intent-filter>
</receiver>
正如我所提到的,如果我在调试版本中同时拥有发送方和接收方应用程序 运行,那么一切都会完美运行,但是如果我 运行 接收方应用程序处于发布模式(没有 proguard 或任何东西)发件人应用程序刚刚获得 RESULT_CANCELLED
结果?
这困扰了我好几天,如有任何想法,我们将不胜感激。
听起来您在声明权限时正在使用 android:protectionLevel="signature"
。如果您 运行 在调试中发送发件人,它会使用您的调试密钥进行签名。接收方将使用发布密钥进行签名。因此,签名将不匹配,接收者将不会被授予许可。
您需要 运行 两者都处于发布模式,以便它们都使用相同的密钥签名。
好的,经过大量搜索和试验后发现这是 Android 的一个相对简单但烦人的安全功能导致了这个错误:
安装的应用程序只有在首次在设备上打开时才能接收广播(普通广播或有序广播)并对其进行操作。在我的例子中,调试版本在 运行 时自动打开,但发布版本没有,并且它没有应用程序图标,仅作为主应用程序的扩展,因此从未打开。
因此,解决方法是为接收应用程序也添加一个应用程序图标,并确保它在设备上 运行。奇怪的是 logcat 中没有生成安全错误,所以除非您知道这种事情,否则很难调试!