1

我按照页面的指南进行操作,并激发了意图。它还发现了条形码。然而当

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {

          showMessage("result", scanResult.toString());

      }
      // else continue with any other code you need in the method

}

到达对话框(showMessage函数基本上只是创建带有标题和文本的对话框)显示以下文本:

格式:null 内容:null 原始字节:(0bytes) 方向:null EC 级别:null

我错过了某些部分还是只是条形码问题?我已经尝试过所有带有条形码的产品,但没有任何变化。

4

2 回答 2

2

// 在 onActivityResult() 中,试试这段代码

if (resultCode == IntentIntegrator.REQUEST_CODE) {

            Log.e("inside Request code~~~~~~~~>", "Barcode>>>>");
            IntentResult scanResult = IntentIntegrator.parseActivityResult(
                    requestCode, resultCode, data);
            if (scanResult == null) {
                       Log.e("Scan Result~~~~~~~~>", "value>>> Null");
                return;
            }
            final String result = scanResult.getContents();
            final String result1 = scanResult.getFormatName();
            if (result != null) {
                handlerBarcode.post(new Runnable() {
                    @Override
                    public void run() {
                        // txtScanResult.setText(result);
                        // txtScanResultFormat.setText(result1);
                        Toast.makeText(Activity_Form_Data_4.this,
                                "Code:" + result + "  Format:" + result1,
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }
于 2013-01-19T08:00:22.093 回答
2

我的项目曾经做过这样的事情:

public class MenuScreen extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu_screen, menu);
        return true;
    }

    public void onScanCodeClick(View view) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.setPackage("com.google.zxing.client.android");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        try {
            startActivityForResult(intent, 0);
        } catch (ActivityNotFoundException aex) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("No Application Found");
            builder.setMessage("We could not find an application to scan QR CODES."
                    + " Would you like to download one from Android Market?");
            builder.setPositiveButton("Yes, Please",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                            marketIntent.setData(Uri
                                    .parse("market://details?id=com.google.zxing.client.android"));
                            startActivity(marketIntent);
                        }
                    });
            builder.setNegativeButton("No, Thanks", null);
            builder.create().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

                Intent intent2 = new Intent();
                intent2.setClass(this, MenuCodeSuccess.class);
                intent2.putExtra("qrDetails", contents);
                startActivity(intent2);

            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

}

onScanCodeClick只是按钮的 onClickListener。您当然可以初始化您的按钮并改用此代码。

这是一个xml布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2" >

    <Button
        android:id="@+id/menuScreen_SCANCODE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onScanCodeClick"
        android:text="@string/scanButton" />

</TableLayout>

请不要在意布局的风格,您所需要的只是在 java 类中。:) 但无论如何它应该可以工作。

于 2013-01-04T11:05:21.357 回答