简体   繁体   中英

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.

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.

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.

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

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.

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

                });
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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