英文:
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 the onDecoded
method for once successfully.
I need to scan QR multiple times, so I tried using a for loop. However, my requirement is to wait for the first QR to be scanned and then proceed to the next scan.
for (int i = 0; i < 10; i++) {
mCodeScanner.startPreview();
// Wait for the first QR to finish scanning and then go for the next iteration
}
I tried using wait()
and notify()
, but it simply crashes on the wait()
statement without throwing any errors.
英文:
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
https://stackoverflow.com/questions/14329654/how-to-notify-another-thread
答案1
得分: 0
"I need to scan QR multiple times..."
为什么不从onDecoded
回调内开始每次扫描?
"I tried using wait() and notify() but it simply crashes..."
如果你想知道出了什么问题,那么你应该在你的问题中展示导致崩溃的代码。
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论