Android OrderedBroadcast在Release Build中不起作用

时间:2015-11-04 09:00:34

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

为什么OrderedBroadcast在应用程序的调试版本中工作但在发布版本中不工作?我发送以下OrderedBroadcast:

 $(function() {
    $('#example2').dataTable({
        "bPaginate": true,
        "bLengthChange": true,
        "bFilter": true,
        "bSort": true,
        "bInfo": true,
        "bAutoWidth": false
    });
});

两个应用程序在AndroidManifest.xml文件中都具有相应的权限,接收者声明如下:

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);

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

这已经困扰了我好几天,所以任何想法都会受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

听起来就像宣布权​​限一样,您正在使用android:protectionLevel="signature"。如果您在调试中运行发件人,则使用您的调试密钥对其进行签名。接收方将使用释放密钥进行签名。因此,签名将不匹配,并且接收者将不被授予许可。

您需要在发布模式下运行它们,因此它们都使用相同的密钥签名。

答案 1 :(得分:1)

好的经过多次搜索和试验后发现这是Android的一个相对简单但烦人的安全功能导致此错误:

如果首次在设备上打开,则安装的应用只能接收广播(普通或有序广播)并对其进行操作。在我的情况下,调试版本在运行时自动打开,但发布版本没有,并且没有应用程序图标仅作为主应用程序的扩展,因此从未打开过。

因此,修复方法是为接收应用程序提供应用程序图标,并确保它在设备上运行。奇怪的是,在logcat中没有生成安全错误,所以除非你知道这种事情,否则很难调试!

相关问题