Android Studio - 当“if”被满足时如何触发意图

我设法让一个由谷歌视觉提供支持的二维码扫描仪工作,并将二维码放入同一活动的文本视图中。


最终目标是在检测到 qrcode 后立即在另一个活动 (QRWebActivity) 的 web 视图中打开 qrcode 中的 url。


在这个阶段,我能够将 qrcode 移动到一个字符串中,然后使用 sendMessage2 在按钮单击时激活的意图推动并在 web 视图中打开。


但我真的很想找到一种方法让它自动打开 QRWebActivity 并将 webview 发送到 'if(qrCodes.size()!=0) 上的 qrCode。


任何帮助都会很棒。


如果我没有使用正确的术语,真的很抱歉,我只是不知道我在做什么,但我真的很想在本周末完成这个应用程序以发布,我已经很接近了。


        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {

            @Override

            public void release() {


            }


            @Override

            public void receiveDetections(Detector.Detections<Barcode> detections) {

                final SparseArray<Barcode> qrCodes = detections.getDetectedItems();


                if(qrCodes.size()!=0)

                {

                    textView.post(new Runnable() {

                        @Override

                        public void run() {

                            Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

                            vibrator.vibrate(1000);

                            textView.setText(qrCodes.valueAt(0).displayValue);

                        }


                    });

                }

            }

        });


    }

    public void sendMessage2 (View view)

    {

        String qrmessage = textView.getText().toString();


        Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);

        intent2.putExtra("EXTRA_QRMESSAGE",qrmessage);

        startActivity(intent2);

    }

}

如果我可以在 qrCodes !=0 时模拟按下按钮并触发“sendMessage2”,那我会这样做......即使我确信有一种更优雅的方式。


弑天下
浏览 135回答 2
2回答

慕码人2483693

从您的代码中,这是我的解决方案:@Overridepublic void receiveDetections(Detector.Detections<Barcode> detections) {&nbsp; &nbsp; final SparseArray<Barcode> qrCodes = detections.getDetectedItems();&nbsp; &nbsp; if (qrCodes.size() != 0) {&nbsp; &nbsp; &nbsp; &nbsp; textView.post(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vibrator.vibrate(1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(qrCodes.valueAt(0).displayValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendMessage2(textView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}

梦里花落0921

所有代码都已经存在,只是不要拆分它:&nbsp; &nbsp; &nbsp; &nbsp; public void receiveDetections(Detector.Detections<Barcode> detections) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final SparseArray<Barcode> qrCodes = detections.getDetectedItems();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(qrCodes.size()!=0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent2.putExtra("EXTRA_QRMESSAGE",qrCodes.valueAt(0).displayValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.post(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vibrator.vibrate(1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(qrCodes.valueAt(0).displayValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java