I have a simple application that opens the Barcode Scanner App by ZXing.
Now, I want to know how much time it takes for a scan to accomplish. Basically, the time when the intent was started up to the time that the result is obtained. Now, I know this is simple, just put a nanoTime object when the intent is executed and another nanoTime when I get the result, subtract the two and multiply to get it in seconds/milliseconds.
However, I have the intent on loop, because I want to scan a sequence of QR Codes and I figured that a for loop is the best way to go about it.
So I have several global time variables long start, end, time;
Here is the code for the intent:
//Multi Scan Button
public Button.OnClickListener onMultiScan = new Button.OnClickListener() {
public void onClick(View v) {
//start = System.nanoTime();
for(int i = 0 ; i < 5 ; i++){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
//start = System.nanoTime();
startActivityForResult(intent, multiScan);
}
}
};
And here is the code to get the input.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//ordinary scan
if (requestCode == ordinaryScan) {
if (resultCode == RESULT_OK) {
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("Time" + " " + /*i + +*/ time);
String contents = data.getStringExtra("SCAN_RESULT");
Toast.makeText(getApplicationContext(), contents, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
//multiple scan
if (requestCode == multiScan) {
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("Time" + " " + /*i + +*/ time);
if (resultCode == RESULT_OK) {
String content = data.getStringExtra("SCAN_RESULT");
//codes = codes + contents + " ";
inputs[counter] = content;
counter += 1;
if(counter == 5){
//output();
verify();
}
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
I commented out the part where I start the timer since I'm not sure where to put them. What happens is if I call it before the loop, I get one result only, if I put it inside the loop, I get multiple results which aren't so far from one another.
Obtaining the time it takes for an intent to finish is easy if it's only called once, but if it's called more than once on loop, it gets tricky.
So, is there a way to properly take the time needed for an intent to open and get the result? I'm open to suggestions even if it means rewriting how I call the activity more than once (I'm currently using a loop).
I also prefer to be able to get the time right after a code is scanned right away, so I know the time between the activity call and the activity result. I don't want to wait for each call to finish before I get the time it took for each call to finish. I want real time update. Thanks.