簡體   English   中英

Android Studio-當滿足“ if”時,如何觸發意圖

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

我已經設法獲得了由Google Vision驅動的二維掃描儀,並將該二維碼放入同一活動的文本視圖中。

最終目標是,一旦檢測到qrcode,就會在另一個活動(QRWebActivity)的Web視圖中打開qrcode中的url。

在此階段,我能夠使用單擊按鈕上sendMessage2激活的意圖將qrcode移至字符串中並推入並在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”,那將對我有幫助...即使我確定還有更優雅的方法。

根據您的代碼,這是我的解決方案:

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

所有代碼已經在那里,只是不要將其拆分:

        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