如何在Android上使用新的IntentIntegrator通过三个不同的按钮对条形码扫描仪进行不同的扫描结果

如何在Android上使用新的IntentIntegrator通过三个不同的按钮对条形码扫描仪进行不同的扫描结果,android,barcode,Android,Barcode,我有三个按钮,只需长按即可通过条形码扫描仪显示我的新管理员扫描。我成功地扫描了它,并在它只有一个按钮的情况下使用扫描的代码做了一些事情 我如何传递一个值或某个东西,当调用protected void on ActivityResult时,它将知道它来自哪个按钮,因此我可以根据长按哪个按钮对它执行不同的操作 我当前的设置如下: button1.setOnLongClickListener(this); button2.setOnLongClickListener(this); button3.se

我有三个按钮,只需长按即可通过条形码扫描仪显示我的新管理员扫描。我成功地扫描了它,并在它只有一个按钮的情况下使用扫描的代码做了一些事情

我如何传递一个值或某个东西,当调用protected void on ActivityResult时,它将知道它来自哪个按钮,因此我可以根据长按哪个按钮对它执行不同的操作

我当前的设置如下:

button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
button3.setOnLongClickListener(this);}
}

public boolean onLongClick(View v) {

    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.initiateScan();
    return true;
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
case IntentIntegrator.REQUEST_CODE:
         if (resultCode == Activity.RESULT_OK) {

            IntentResult intentResult = 
               IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

            if (intentResult != null) {

               String contents = intentResult.getContents();
               String format = intentResult.getFormatName();
               //do stuff with the scan. But I want to do different stuff depending on which button was pressed. 
两种选择:

首先,您可以修改IntentIntegrator源代码,以便它将不同的REQUEST_代码作为构造函数参数,或者只添加一个setRequestCode参数。然后在onActivityResult中,检查返回了哪个请求代码。每个按钮都会返回不同的请求代码。这将是建议的方法


第二个选项是骇人听闻的:在代码中,创建一个成员变量,跟踪最后按下的按钮。检查onlong click传入的视图是否与button1、button2或button3匹配,并在onActivityResult中使用该信息选择如何处理从条形码扫描仪传回的结果。

谢谢。我不知道为什么,但我有时会忘记变量的简单威力。创建一个int,然后根据视图进行更改,然后在对扫描信息进行填充之前读取int变量。