zxing onActivityResults not firing

2019-09-12 15:09发布

问题:

I'm trying to pull data from a QR code via zxing using the following code (basically copypasted from a tutorial, with names changed to protect the innocent):

            //Check for Barcode scanner, if not found put up an alert that allows user to install it.
            PackageManager pm = getPackageManager();
            try {
                ApplicationInfo appInfo = pm.getApplicationInfo("com.google.zxing.client.android", 0);
                Intent intent = new Intent(
                "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");//for Qr code, its "QR_CODE_MODE" instead of "PRODUCT_MODE"
                intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history
                startActivityForResult(intent, 0);
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                 new AlertDialog.Builder(BitcoinAddressUtilityActivity.this)
                    .setTitle("WARNING:")
                    .setMessage("You don't have Barcode Scanner installed. Please install it.")
                     .setCancelable(false)
                    .setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {         
                                  Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
                                  startActivity(new Intent(Intent.ACTION_VIEW, uri));
                            }
                    })
                    .show();

            }


        }

        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
               if (requestCode == 0) {
                  if (resultCode == RESULT_OK) {
                     String contents = intent.getStringExtra("SCAN_RESULT");
                     String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                     // Handle successful scan
                     EditText passphrase = (EditText) findViewById(R.id.txtPassphrase);
                     passphrase.setText(contents);
                  } else if (resultCode == RESULT_CANCELED) {
                     // Handle cancel
                  }
               }
            }


    });

It executes just fine without errors, but onActivityResults never fires, I even get a warning:

The method onActivityResult(int, int, Intent) from the type new View.OnClickListener(){} is never used locally

I'll fully admit that I'm an activity/intent noob, so if someone wants to answer in the form of a tutorial I'll gladly accept that - I don't just want a code fix, I want to know why it's not working in the first place.

回答1:

You have following problem in your code

you have written public void onActivityResult(int requestCode, int resultCode, Intent intent) method inside button onclick listener. move it outside.

your code snippet should look something like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            ----
            ----
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == 0) {
              if (resultCode == RESULT_OK) {
                 String contents = intent.getStringExtra("SCAN_RESULT");
                 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                 // Handle successful scan
                 EditText passphrase = (EditText) findViewById(R.id.txtPassphrase);
                 passphrase.setText(contents);
              } else if (resultCode == RESULT_CANCELED) {
                 // Handle cancel
              }
           }

}