BroadcastReceiver尝试在无序广播期间返回结果

时间:2015-06-03 09:59:33

标签: android broadcastreceiver

在我的app广播接收器错误是原因。我正在创建谷歌云消息传递演示

我的LogCat是

06-03 15:09:37.584: E/BroadcastReceiver(15498): BroadcastReceiver trying to return result during a non-ordered broadcast
06-03 15:09:37.584: E/BroadcastReceiver(15498): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.content.BroadcastReceiver.setResult(BroadcastReceiver.java:658)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:56)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.app.ActivityThread.access$1700(ActivityThread.java:151)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
06-03 15:09:37.584: E/BroadcastReceiver(15498):     at android.os.Handler.dispatchMessage(Handler.java:102)

这是广播接收器

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE);
            String newName = intent.getExtras().getString("name");
            String newIMEI = intent.getExtras().getString("imei");

            Log.i("GCMBroadcast","Broadcast called."+newIMEI);

            // Waking up mobile if it is sleeping
            aController.acquireWakeLock(getApplicationContext());

            String msg = lblMessage.getText().toString();
            msg = newName+" : "+newMessage+"\n"+msg;
            // Display message on the screen
            lblMessage.setText(msg);
            //lblMessage.append("\n"+newName+" : "+newMessage);         


            Toast.makeText(getApplicationContext(), 
                    "Got Message: " + newMessage, 
                    Toast.LENGTH_LONG).show();

            /************************************/
             //CustomListViewValuesArr.clear();
             int rowCount = DBAdapter.validateNewMessageUserData(newIMEI);
             Log.i("GCMBroadcast", "rowCount:"+rowCount);
             if(rowCount <= 1 ){
                    final UserData schedSpinner = new UserData();

                    /******* Firstly take data in model object ********/
                    schedSpinner.setName(newName);
                    schedSpinner.setIMEI(newIMEI);


                  /******** Take Model Object in ArrayList **********/
                  CustomListViewValuesArr.add(schedSpinner);
                  adapter.notifyDataSetChanged();

                }

                //CustomListViewValuesArr.addAll(SpinnerUserData);



            /************************************/

            // Releasing wake lock
            aController.releaseWakeLock();
        }
    };

请告诉我如何解决错误我试图在互联网上找到但确切无法找到解决方案。

1 个答案:

答案 0 :(得分:1)

首先,我不是Android开发方面的专家,尤其是广播接收器。

我浏览了你在这里发布的代码,我的初学者大脑找不到任何设置结果的语句。我无法确定异常命中你的陈述,所以我的回答可能很模糊。

根据BroadcastReceiver文档,以异步方式运行接收器效率更高,但意味着接收方无法使用结果或中止BroadcastReceiver类的API。

该问题的一个解决方案是使用Context.sendOrderedBroadcast,但由于我们正在处理GoogleCloudMessages,因此我们不能自由处理广播发送部分。

已订购Google Cloud Messaging广播。只是在这种情况下收到的广播是非订购的。

有一个函数isOrderedBroadcast()可以用作变通办法,这样就可以避免将结果设置为无序广播。