Android OrderedBroadcast在Release Build中不起作用

Apq*_*pqu 6 android broadcastreceiver android-intent android-broadcast android-studio

为什么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);
Run Code Online (Sandbox Code Playgroud)

两个应用程序在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>
Run Code Online (Sandbox Code Playgroud)

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

这已经困扰了我好几天,所以任何想法都会非常感激.

Apq*_*pqu 1

好吧,经过大量搜索和试验,结果发现这是 Android 的一个相对简单但令人讨厌的安全功能导致了这个错误:

安装的应用程序仅在设备上首次打开时才能接收广播(正常广播或有序广播)并对其执行操作。就我而言,调试版本在运行时会自动打开,但发布版本不会,并且它没有仅充当主应用程序扩展的应用程序图标,因此从未打开。

因此,解决方案是为接收应用程序提供一个应用程序图标,并确保它在设备上运行。奇怪的是,logcat 中不会生成安全错误,因此除非您了解这种情况,否则很难调试!