zxing-android-embedded
zxing-android-embedded copied to clipboard
Getting callback of barcodeScannerView again in decodeSingle on destroy of the Activity
Description of the problem:
I just modify CustomScannerActivity class of sample project according to my requirement. On getting the result of QR code I showed a progressDialog and finish the Activity after 3 seconds of that but on destroy of activity, I'm getting callback of barcodeScannerView again. How can I stop callback after getting one?
Here is the modified code.
public class CustomScannerActivity extends Activity implements
DecoratedBarcodeView.TorchListener, BarcodeCallback {
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private Button switchFlashlightButton;
private ViewfinderView viewfinderView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
handler = new Handler();
barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.zxing_barcode_scanner);
barcodeScannerView.setTorchListener(this);
switchFlashlightButton = (Button) findViewById(R.id.switch_flashlight);
viewfinderView = (ViewfinderView) findViewById(R.id.zxing_viewfinder_view);
// if the device does not have flashlight in its camera,
// then remove the switch flashlight button...
if (!hasFlash()) {
switchFlashlightButton.setVisibility(View.GONE);
}
capture = new CaptureManager(this, barcodeScannerView);
// capture.initializeFromIntent(getIntent(), savedInstanceState);
// capture.decode();
barcodeScannerView.decodeSingle(this);
changeMaskColor(null);
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
hideProgressBar();
Log.v("LogTrack", "onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
/**
* Check if the device's camera has a Flashlight.
*
* @return true if there is Flashlight, otherwise false.
*/
private boolean hasFlash() {
return getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
public void switchFlashlight(View view) {
if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) {
barcodeScannerView.setTorchOn();
} else {
barcodeScannerView.setTorchOff();
}
}
public void changeMaskColor(View view) {
Random rnd = new Random();
int color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
viewfinderView.setMaskColor(color);
}
@Override
public void onTorchOn() {
switchFlashlightButton.setText(R.string.turn_off_flashlight);
}
@Override
public void onTorchOff() {
switchFlashlightButton.setText(R.string.turn_on_flashlight);
}
private Handler handler;
@Override
public void barcodeResult(BarcodeResult result) {
Log.v("LogTrack", "barcodeResult");
if (result != null) {
Log.v("LogTrack", "result = " + result.getResult().toString());
try{
showProgressBar(result.getResult().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 3000);
}catch (Exception e){
Log.v("LogTrack", "e = " + e.getMessage());
e.printStackTrace();
}
}
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
private ProgressDialog progressDialog;
public void showProgressBar(String text) {
if (!isFinishing()){
progressDialog = ProgressDialog.show(this, "", text, false, false);
}
}
public void hideProgressBar() {
if (!isFinishing()) {
if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); }
}
}
}