ZXING intent request code (identifying my intent)

2019-06-24 02:49发布

问题:

In my Android APP, whenever I need to call many different ActivitiesForResult from the same Activity, I do it like this:

public void firstMethod() {
    int requestCode = 1;
    Intent intent = new Intent(SomeCode1.class);
    startActivityForResult(intent,requestCode);
}

public void secondMethod()  {
    int requestCode = 2;
    Intent intent = new Intent(SomeCode2.class);
    startActivityForResult(intent,requestCode);
}

And to know which intent it came from, I recognize them like this:

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode) {
        case 1: {
              // some code 
        } case 2: {
              // some code
        }
  }

I am trying to call the ZXING barcode Scanner twice from the same activity, and I do not know how to set a request code with it.

    IntentIntegrator intentintegrator= new IntentIntegrator(this);
    IntentIntegrator.initiateScan(ZxingIntent.QR_CODE_TYPES);

Does anybody know how to accomplish this? Do I need to modify the IntentIntegrator code?

回答1:

I see 2 solutions. One would be creating a new activity, just to call the IntentIntegrator and putting the requestCode to this new activity.

Second option was to modify the IntentIntegrator - which is what I did.

I removed the final attribute,

// public static final int REQUEST_CODE = 0x0000c0de;
public static int REQUEST_CODE = 0x0000c0de;

added the function to set the request code:

  public void setRequestCode(int requestCode) {
    REQUEST_CODE = requestCode;
  }

and am calling the Barcode Scanner like this:

    int requestCode = 2;
    IntentIntegrator intentintegrator= new IntentIntegrator (this);
    intentintegrator.setRequestCode(requestCode);
    intentintegrator.initiateScan(ZxingIntent.QR_CODE_TYPES);

I do not know what the RequestCode 0x0000c0de is for and why it is final, but the app seems to work.