Android - Measuring the time between opening an activity via intent and when results are received

StackOverflow https://stackoverflow.com/questions/14009466

Domanda

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.

È stato utile?

Soluzione 2

Okay, found a way to make it work. It's a bit unrefined to be honest but it works and updates real time. The basic concept is to have a multiScan requestCode that is incremented every time that it gives a result as handled in onActivityResult method. Then there's an if statement that checks if the multiScan requestCode has reached the maximum number of iterations desired.

So here are the global variables:

int multiScan = 1;
int multiScanBase = 1;
long start, end, time;

And then the multiScanButton would look something like:

//Multi Scan Button
public Button.OnClickListener onMultiScan = new Button.OnClickListener() {
    public void onClick(View v) {

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

    }
};

Then the onActivityResult method would look something like this:

//multiple scan
if (requestCode < multiScanBase + 5 ) {
        end = System.nanoTime();

        time = (end - start) / 1000000;
        System.out.println("Request Code = " + multiScan);

        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
        }

            //This is the part where I check for the requestCode
            //In this example, I wanted to iterate the activity 5 times
            //So if the request code is still less than the base + number of iterations
            //Increment the multiScan requestCode and run the method again
            //In this way, the time for each scan is given in real time
        if(requestCode < multiScanBase + 5){
            multiScan += 1;
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.setPackage("com.google.zxing.client.android");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

            startActivityForResult(intent, multiScan);

        }
 }

So there. The code works for me and updates are given in real time. Thanks for the ideas on how to do this.

Merry Christmas everyone.

Altri suggerimenti

If you still want to use the loop, a possible approach would be not to use a single multiscan requestcode but one for each run of the loop.

Something like startActivityForResult(intent, MULTISCAN_BASE + i);

In this way you will be able to track all the runs separately, because you will be able to recognize which run the result refers to :

i = requestCode - MULTISCAN_BASE

The final step is putting your start time in an array, one for each cycle of the loop.

int[] start = new int[5];

Checking when you are done and get the sum of the times is then trivial.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top