我想扫描二维码后进行下一步操作?我应该在哪里添加 Intent?

I want to proceed to next action after scanning QR Code? Where should I add Intent?

我需要在 android 项目上实现二维码扫描器。我实现了二维码,但是我不知道如何在读取二维码后切换到另一个动作。

我想创建一个 Intent 以移动到新操作,但我不知道在哪里添加它。 我需要应用程序在检测到并读取二维码后立即切换到另一个操作。

代码:

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

    CodeScannerView scannerViewTest = findViewById(R.id.scanner_view_test);
    codeScannerTest = new CodeScanner(this, scannerViewTest);

    askPermission();
    if(CameraPermission) {


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

                codeScannerTest.startPreview();
            }
        });

        codeScannerTest.setDecodeCallback(new DecodeCallback() {
            @Override
            public void onDecoded(@NonNull Result result) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(ScanTest.this, result.getText(), Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });
    }

}

private void askPermission(){

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(ScanTest.this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERM);

        }else {

            codeScannerTest.startPreview();
            CameraPermission = true;
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if(requestCode == CAMERA_PERM){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            codeScannerTest.startPreview();
            CameraPermission = true;
        }else{

            if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
                new AlertDialog.Builder(this)
                        .setTitle("Permission")
                        .setMessage("Please provide the camera permission for using all the features of the app")
                        .setPositiveButton("Proceed", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                ActivityCompat.requestPermissions(ScanTest.this, new String[]{ Manifest.permission.CAMERA}, CAMERA_PERM);
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                }).create().show();

            } else {

                new AlertDialog.Builder(this)
                        .setTitle("Permission")
                        .setMessage("You have denied some permissions. Allow all permissions at [Settings] > [Permissions]")
                        .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                dialog.dismiss();
                                Intent settings_intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null));
                                settings_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(settings_intent);
                                finish();

                            }
                        }).setNegativeButton("No, Exit app", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        finish();
                    }
                }).create().show();

            }

        }
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onPause() {
    if(CameraPermission){
        codeScannerTest.releaseResources();
    }
    super.onPause();
}

需要在onDecoded方法处添加

codeScannerTest.setDecodeCallback(new DecodeCallback() {
        @Override
        public void onDecoded(@NonNull Result result) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent intent = Intent(this, YouNextActivity.class);
                    intent.putExtra("barcode", result.getText());
                    startActivity(intent);
                    //Toast.makeText(ScanTest.this, result.getText(), Toast.LENGTH_SHORT).show();

                }
            });
        }
    });

您可以使用 Intent.getExtra("barcode") 并且可以在您的下一个 Activity 中获取值。