zxing onActivityResults没有发射 [英] zxing onActivityResults not firing

查看:221
本文介绍了zxing onActivityResults没有发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用下面的code zxing拉从QR code数据(从教程基本上copypasted,其名称更改为保护无辜者):

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
                  }
               }
            }


    });

据执行没有错误就好了,但从来没有onActivityResults火灾,我甚至收到了警告:

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

的方法的onActivityResult(INT,INT,意向)来自从未在本地使用的类型新View.OnClickListener(){}

我完全承认,我是一个活动/意图小白,所以如果有人想在教程的形式来回答,我会欣然接受 - 我不只是想要一个code修复,我想知道为什么它不是摆在首位的工作。

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.

推荐答案

您已经按照您的code问题

You have following problem in your code

您已经写了公共无效的onActivityResult(INT申请code,INT结果code,意图意图)按钮的onclick监听器里方法。外移动。

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

您code段应是这个样子

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
              }
           }

}

这篇关于zxing onActivityResults没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆