简体   繁体   English

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

[英]Android Studio - How can I trigger an intent when an 'if' is fulfilled

I've managed to get a qr scanner powered by google vision working and placing the qrcode into a text view on the same activity. 我已经设法获得了由Google Vision驱动的二维扫描仪,并将该二维码放入同一活动的文本视图中。

The end goal is to have the url in the qrcode open up in a webview in another activity (QRWebActivity) as soon as a qrcode is detected. 最终目标是,一旦检测到qrcode,就会在另一个活动(QRWebActivity)的Web视图中打开qrcode中的url。

At this stage I was able to move the qrcode into a string and push across and open in the webview using intents activated by sendMessage2 on button click. 在此阶段,我能够使用单击按钮上sendMessage2激活的意图将qrcode移至字符串中并推入并在Web视图中打开。

But I really want to find a way to have it just automatically open the QRWebActivity and send the webview to the qrCode on 'if(qrCodes.size()!=0). 但是我真的很想找到一种方法,使其自动打开QRWebActivity并将webview发送到'if(qrCodes.size()!= 0)上的qrCode。

Any help would be amazing. 任何帮助都将是惊人的。

Really sorry if I'm not using the right terminology, I just don't know what I'm doing but really keen to finish this app by the end of the week for release and I'm so close. 非常抱歉,如果我使用的术语不正确,我只是不知道自己在做什么,但真的很想在本周末完成该应用程序的发布,所以我非常接近。

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

If I could just simulate pressing the button and triggering 'sendMessage2' when qrCodes !=0 that would do me... even though I'm sure there's a more elegant way. 如果我可以模拟当qrCodes!= 0时按下按钮并触发“ sendMessage2”,那将对我有帮助...即使我确定还有更优雅的方法。

From your code, here is my solution: 根据您的代码,这是我的解决方案:

@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);
                sendMessage2(textView);
            }
        });
    }
}

All the code is already there, just dont split it up: 所有代码已经在那里,只是不要将其拆分:

        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrCodes = detections.getDetectedItems();

            if(qrCodes.size()!=0)
            {
                Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);
                intent2.putExtra("EXTRA_QRMESSAGE",qrCodes.valueAt(0).displayValue);
                startActivity(intent2);
                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);
                    }

                });
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM