如何使用eventchannel从本机服务的BroadcastReceiver接收数据?错误:已提交回复

jjhzyzn0  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(184)

假设我有一个flatter应用程序,当我慢慢单击时,它会在打开代码时成功地呈现result.success(条形码)。但是应用程序崩溃当我快速点击时,它意味着mult~result.success(“”)result.success(“”)导致崩溃。我想通过通道方法日志实现连接:

java.lang.IllegalStateException: Reply already submitted

省道代码:

@override
  void initState() {
    initBroad();
    super.initState();
  }
   @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("home"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            ElevatedButton(
              child: Text("click me"),
              onPressed: getBatteryInfo,
            ),
            Text("data: $_res"),
          ],
        ),
      ),
    );
  }

  void getBatteryInfo() async {
    final result = await platform.invokeMethod("getBatteryInfo");
    setState(() {
      _res = result;
      print(_res);
    });
  }

   void initBroad() async {
    final res = await platform.invokeMethod("register");
    setState(() {
       _res = res;
      print(_res);
    });
  }

mainactivity.java:

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "test.com/native";
    private MethodChannel.Result ret;
    String barcode = "";
    String  m_Broadcastname = "com.barcode.sendBroadcast";
    MyCodeReceiver receiver = new MyCodeReceiver();
    @Override
    public synchronized void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);
        methodChannel.setMethodCallHandler(
                (call, result)->{
                    ret = result;
                    switch(call.method)
                    {
                        case "getBatteryInfo" :                                        
                  // ret.success(getAsyncData()); //that's success return, whatever quick or slowly click                       
                               //when I open the code,I quickly double click,happened App crashed

                               // Intent intent = new Intent();
                               // intent.setAction("com.barcode.sendBroadcastScan");
                               // sendBroadcast(intent);                           
                            break;
                        case "register" :
                            ret.success("success register");
                            registBroad();
                            break;
                        case "dispose" :
                            dispose();
                            break;
                        default :
                            ret.notImplemented();
                    }
                }
        );

    }
    private void registBroad(){
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(m_Broadcastname);
        registerReceiver(receiver, intentFilter);
    }

    public class MyCodeReceiver extends BroadcastReceiver
    {
        private static final String TAG = "MycodeReceiver";
        @Override
        public synchronized void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(m_Broadcastname)) {
                String str = intent.getStringExtra("BARCODE");
                // codeScan, It's need about 1-2s until I get the code 
                if (!"".equals(str)) {
                    barcode = str;
                    ret.success(barcode);
                }
            }
        }
    }
    private void dispose(){
        unregisterReceiver(receiver);
    }
    private int getAsyncData() throws InterruptedException {
        Thread.sleep(2000);
        return 123;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题