Android OrderedBroadcast 在发布版本中不起作用

标签 android android-intent android-studio broadcastreceiver android-broadcast

为什么 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>

正如我所提到的,如果我在调试版本中同时运行发送方和接收方应用程序,那么一切都会完美运行,但是如果我在 Release模式下运行接收方应用程序(没有 proguard 或任何东西),发送方应用程序只会获得 RESULT_CANCELLED 结果?

这困扰了我好几天,所以任何想法都将不胜感激。

最佳答案

这听起来像是在声明权限时,您正在使用 android:protectionLevel="signature"。如果您在调试中运行发送器,它会使用您的调试 key 进行签名。接收方将使用发布 key 进行签名。因此,签名将不匹配,接收者将不会被授予许可。

您需要在 Release模式下运行两者,以便它们都使用相同的 key 签名。

关于Android OrderedBroadcast 在发布版本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517566/

相关文章:

android - Firebase 数据库查询 : return latest added object in which the child has a value

android - 自动化gradle从代码构建应用程序包(.apk)

android - 如何在android studio中添加新的导入

android - 访问 'viewBinding' 超出其访问权限

android - 如何在android中保存图像和表面 View ?

java - 列出 AlarmManager 中的所有 PendingIntents

java - 底部导航 View 随着 windowSoftInputMode=adjustResize 上升

java - 从我的应用程序 Android 启动另一个应用程序

android - 替换 android Intent 选择器对话框

android - 安全过滤 CALL_PRIVILEGED