I need to implement a QR Code scanner on an android project. I implemented the QR Code, however, I do not know how to switch to another action after QR is read.
I want to create an Intent to move to new action but I do not know where to add it. I need the app to switch to another action as soon as it detects and reads QR Code.
code:
@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();
}
You need to add it at the onDecoded
method
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();
}
});
}
});
You can use the Intent.getExtra("barcode")
and can get the value in your next Activity.