修改zxing里的CaptureManager,使扫描后不结束activity。
扫描activity的代码如下:
package com.byxc.scanning;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
/**
* 扫描二维码
* Created by ypp on 2018/5/16.
*/
public class ScanningActivity extends Activity {
private final String TAG="ScanningActivity";
private CustomerCaptureManager captureManager;//扫描
private DecoratedBarcodeView dbv_custom;
/**
* 当前闪光灯是否打开了
*/
private boolean isTorchOn=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_scanning);
dbv_custom=(DecoratedBarcodeView)findViewById(R.id.dbv_custom);
//重要代码,初始化捕获
initCapture();
// isScanHandler.sendEmptyMessageDelayed(0,30000);//30秒未扫描到,提示
}
/**
* 初始化捕捉
*/
private void initCapture(){
if(captureManager!=null){
captureManager.onDestroy();
captureManager=null;
}
captureManager = new CustomerCaptureManager(this,dbv_custom,scanningListener);
captureManager.initializeFromIntent(getIntent(),null);
captureManager.decode();
}
/**
* 扫描结果监听
*/
private CustomerCaptureManager.ScanningListener scanningListener=new CustomerCaptureManager.ScanningListener(){
@Override
public void scanResult(String result) {
HLog.toast(ScanningActivity.this,result);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
initCapture();
}
},500);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
captureManager.onSaveInstanceState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return dbv_custom.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
public void onResume()
{
super.onResume();
captureManager.onResume();
}
public void onPause()
{
super.onPause();
captureManager.onPause();
}
/**
* 切换闪光灯
* @param view
*/
public void openOrCloseTorch(View view) {
if(isTorchOn){
if(hasTorch()){
TextView flash_btn=(TextView)findViewById(R.id.flash_btn);
flash_btn.setBackgroundResource(R.drawable.close_flash_light_bg);
flash_btn.setText(getResources().getString(R.string.open_flash_light));
dbv_custom.setTorchOff();
}
}else{
if(hasTorch()){
TextView flash_btn=(TextView)findViewById(R.id.flash_btn);
flash_btn.setBackgroundResource(R.drawable.open_flash_light_bg);
flash_btn.setText(getResources().getString(R.string.close_flash_light));
dbv_custom.setTorchOn();
}
}
isTorchOn=!isTorchOn;
}
/**
* 判断是否有闪光灯
* @return
*/
public boolean hasTorch(){
return getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
}
CustomerCaptureManager代码: