Android手持机扫码出入库的开发详解-2.入库扫码

52 阅读1小时+

Android手持机扫码出入库的开发详解-2.入库扫码

入库扫码界面

入库扫码程序图

flowchart TD
    A[进入入库扫码界面] --> B[初始化扫码设备]
    B --> C[等待扫描材料条码]
    C --> D[验证条码格式]
    D -->|格式错误| E[提示错误信息]
    D -->|格式正确| F[显示材料信息]
    F --> G[等待扫描货位条码]
    G --> H[验证货位格式]
    H -->|格式错误| I[提示错误信息]
    H -->|格式正确| J[显示货位信息]
    J --> K[保存入库记录]
    K --> L[判断是否继续]
    L -->|继续| C
    L -->|结束| M[返回主界面]

入库扫码源代码

// ScanCodeInStorage.java - 入库扫码核心代码
public class ScanCodeInStorage extends Activity {
    private ScanDevice sm = null;
    private String strCode = null;
    private String strHwh = null;
    private String userName;
    private InStorageDAO inStorageDAO;

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

        // 获取用户信息
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        this.userName = bundle.getString("userName");

        // 初始化扫码设备
        sm = new ScanDevice();
        sm.initDevice(this);
        sm.startScan();

        // 初始化数据库访问对象
        inStorageDAO = new InStorageDAOSqliteImpl(this);

        // 注册扫码广播接收器
        registerReceiver(mScanReceiver, new IntentFilter(ScanDevice.SCAN_ACTION));
    }

    // 扫码广播接收器
    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ScanDevice.SCAN_ACTION)) {
                byte[] barocode = intent.getByteArrayExtra("barocode");
                int barocodelen = intent.getIntExtra("length", 0);
                String barcodeStr = new String(barocode, 0, barocodelen);

                // 条码处理逻辑
                handleBarcode(barcodeStr);
            }
        }
    };

    // 处理扫描到的条码
    private void handleBarcode(String barcode) {
        if (barcode.length() == 21) {
            // 材料条码 (21位)
            this.strCode = barcode;
            // 查询并显示材料信息
            queryMaterialInfo(barcode);
        } else if (barcode.length() == 8 || barcode.length() == 9) {
            // 货位条码 (8位或9位)
            this.strHwh = barcode;
            // 查询并显示货位信息
            queryLocationInfo(barcode);
        } else {
            Toast.makeText(this, "条码格式不正确,请重新扫描", Toast.LENGTH_SHORT).show();
        }

        // 当材料和货位都扫描完成后,保存入库记录
        if (strCode != null && strHwh != null) {
            saveInStorageRecord();
        }
    }

    // 保存入库记录
    private void saveInStorageRecord() {
        InStorage inStorage = new InStorage();
        inStorage.setCode(this.strCode);
        inStorage.setHwh(this.strHwh);
        inStorage.setName(this.userName);
        inStorage.setDateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        try {
            inStorageDAO.insertData(inStorage);
            Toast.makeText(this, "入库记录保存成功", Toast.LENGTH_SHORT).show();
            // 清空已扫描信息,准备下一次扫描
            this.strCode = null;
            this.strHwh = null;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "入库记录保存失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播接收器
        unregisterReceiver(mScanReceiver);
        // 关闭扫码设备
        if (sm != null) {
            sm.stopScan();
            sm.closeDevice();
        }
    }
}