Search code examples
javaandroidmultithreadingqr-codebarcode-scanner

Android : How to wait for QR code scanner to finish and then continue for next scan in main thread


I am new to android development

I use this QR code scanner library

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CodeScannerView scannerView = findViewById(R.id.scanner_view);
        mCodeScanner = new CodeScanner(this, scannerView);
        mCodeScanner.setDecodeCallback(new DecodeCallback() {
            @Override
            public void onDecoded(@NonNull final Result result) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        scannerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCodeScanner.startPreview();
            }
        });       
    }

I am able to get the qr code result in onDecoded method for once successfully.

I need to scan QR multiple times so I tried using for loop, but my need is wait for the first QR to be scanned, and then go for next scan.

for(int i =0;i<10;i++) {
   mCodeScanner.startPreview();
   // wait for first QR to finish scanning and then go for next iteration
}

I tried using wait() and notify() but it simply crashes on wait() statement without any error throwing How to notify another thread


Solution

  • I need to scan QR multiple times...

    Why not start each next scan from within the onDecoded callback?

    I tried using wait() and notify() but it simply crashes...

    If you are asking what went wrong there, then you should show the code that crashes in your question.