QR码将数据发送到活动

时间:2018-06-01 15:33:13

标签: java android qr-code

我正在做的应用程序扫描QR码并将扫描数据发送到另一个活动。

我无法通过QR扫描将数据发送到其他活动。我认为这是因为方法在活动开始之前返回。但我无法解决这个问题

以下是代码:

public class ChooseActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose);

    final Activity activity = this;

    _flag = false;

img1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
            intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            intentIntegrator.setPrompt("Scan");
            intentIntegrator.setCameraId(0);
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.initiateScan();


            if (_flag == true)
            {
                Intent intent = new Intent(ChooseActivity.this, ReviewActivity.class);
                startActivity(intent);
            }

        }
    });
 }

onActivity结果:

@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, 
 resultCode, data);
    if (result != null)
    {
        if (result.getContents() == null)
        {
            Toast.makeText(this, "Вы отменили сканирование", 
 Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, result.getContents(), 
 Toast.LENGTH_SHORT).show();


            String string = result.getContents().toString();
            String[] parts = string.split("-");
            part1 = parts[0];
            part2 = parts[1];
            part3 = parts[2]; 

            String flag = "true";

            Intent intent = new Intent(ChooseActivity.this, 
  ReviewActivity.class);
            intent.putExtra("route", part1);
            intent.putExtra("driver", part2);
            intent.putExtra("car", part3);
            intent.putExtra("flag", flag);
            intent.putExtra("key", s1);
            intent.putExtra("key2", s2);

            _flag = true;

        }
    }
    super.onActivityResult(requestCode, resultCode, data);

}

当扫描QR码时,另一项活动在数据发送到此处之前开始。 这就是问题

1 个答案:

答案 0 :(得分:0)

将此代码实施到您的项目中: -

 img1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
        intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        intentIntegrator.setPrompt("Scan");
        intentIntegrator.setCameraId(0);
        intentIntegrator.setBeepEnabled(false);
        intentIntegrator.initiateScan();

    }
});