Answer the question
In order to leave comments, you need to log in
How to automatically open Activity after scanning?
My application has a "zxing" scanner. I need "activity_scanner" to open after scanning.
Scanner code:
public class ScannerActivity extends AppCompatActivity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
}
/*Запуск сканера qr-кода:*/
public void scanQR(View v) {
try {
/*Запускаем переход на com.google.zxing.client.android.SCAN с помощью intent:*/
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
/*Предлагаем загрузить с Play Market:*/
showDialog(ScannerActivity.this, "Сканнер не найден", "Установить сканер с Play Market?", "Да", "Нет").show();
}
}
/*alert dialog для перехода к загрузке приложения сканера:*/
private static AlertDialog showDialog(final Activity act, CharSequence title,
CharSequence message,CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
/*Ссылка поискового запроса для загрузки приложения:*/
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
/*Обрабатываем результат, полученный от приложения сканера:*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
/*Получаем данные после работы сканера и выводим их в Toast сообщении:*/
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast toast = Toast.makeText(this, "Содержание: " + contents + " Формат: " + format, Toast.LENGTH_LONG);
toast.show();
startActivity(intent);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
In the "onActivityResult()" method, the "intent" argument is only meant to return the scanned barcode.
No "startActivity(intent)" needs to be done with it.
If you need to leave the activity on the screen that was before the scanner was launched, this will happen automatically.
And if you want to launch another activity - make a new intent:
Intent next = new Intent(this, NextActivity.class);
startActivity(next);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question