80,351
社区成员
发帖
与我相关
我的任务
分享
package com.juri.juriscanner;
import java.util.ArrayList;
import com.zltd.decoder.Constants; //调用sdk
import com.zltd.industry.ScannerManager; //这个也是sdk的
import com.juri.juriscanner.R;
//import android.annotation.SuppressLint;SoundUtils //这个是声音接口
import android.app.Activity; //下面的是系统自带的
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler; //消息处理 Handler对像
import android.os.Message;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
public abstract class BaseActivity extends Activity implements //调肜了iscannerstatuslisterner接口
ScannerManager.IScannerStatusListener {
protected static final int UPDATE_LIST = 4096; //4096
protected static final int UPDATE_NUMBER = 4097; //4097
protected ScannerManager mScannerManager;
protected SoundUtils mSoundUtils;
protected ArrayList<String> mBarcodeList = new ArrayList<String>(); //条码列表列表
protected ArrayAdapter<String> mListAdaper; //字符串适配器
protected int pressed = 0; //计数器,用来计算按键数
protected int scanned = 0; //计数器,用来计算扫描数
protected int decoderType = 0;
//onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerManager = ScannerManager.getInstance(); //实例化
decoderType = mScannerManager.getDecoderType(); //解码类型
mSoundUtils = SoundUtils.getInstance();//声音对象
mSoundUtils.init(this); //声音初始化
mListAdaper = new ArrayAdapter<String>(this, R.layout.list_item, mBarcodeList);
}
public void onResume() {
super.onResume();
//2.返回后继续代码
int res = mScannerManager.connectDecoderSRV(); //连接驱动
mScannerManager.addScannerStatusListener(this);
if (decoderType == Constants.DECODER_ONED_SCAN) { //解码类型
if (!mScannerManager.getScannerEnable()) {
new AlertDialog.Builder(this)
.setTitle(R.string.action_settings)
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage(R.string.scan_message)
.setPositiveButton(R.string.dialog_ok, new OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
closeSelf();
}
})
.setCancelable(false)
.show();
}
}
}
// 4.APP暂停代码
public void onPause() {
mScannerManager.removeScannerStatusListener(this);
mScannerManager.disconnectDecoderSRV(); //断开连接
super.onPause();
}
protected void closeSelf() {
this.finish();
}
//更新UI,这里HANDLDER是因为安卓子线程不能更新UI,必须通过HANLDER方向
//HANDLER接收消息
//@SuppressLint("HandlerLeak")
// protected Handler mHandle = new Handler() {
//// public void handleMessage(Message msg) {
//// switch (msg.what) {
//// case UPDATE_LIST:
//// scanned++; //次数增加
//// mSoundUtils.success(); //播放声音
//// updateList((String) msg.obj); //添加数据到列表框
//// case UPDATE_NUMBER: //增加数量,显示在前台标签
//// updateCount();
//// break;
//// default:
//// break;
//// }
//// }
//// };
protected Handler mHandle=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case 4096:
scanned++; //次数增加
mSoundUtils.success();
updateList((String) msg.obj); //添加数据到列表框
case 4097:
//updateCount();
break;
default:
break;
}
return false;
}
});
public abstract void updateCount();
public abstract void updateList(String data);
//清除消息
public void clear() {
mBarcodeList.clear(); //清除列表
mListAdaper.notifyDataSetChanged(); //适配器清空
pressed = 0;
scanned = 0;
mHandle.sendEmptyMessage(4097); //发送数据
}
//3.结果改变,sdk事件
@Override
public void onScannerResultChanage(byte[] arg0) {
String data = new String(arg0);
Message msg = mHandle.obtainMessage(4096, data); //获取消息
mHandle.sendMessage(msg);
}
//扫描状态改变
@Override
public void onScannerStatusChanage(int arg0) {
// TODO Auto-generated method stub
}
}
package com.juri.juriscanner;
import com.zltd.industry.ScannerManager; //调用sdk接口
import com.juri.juriscanner.R;
import android.content.Intent; //intent方法
import android.os.Bundle;
import android.view.KeyEvent; //键盘事件
import android.view.View;
import android.widget.Button; //下面这些都是控件
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends BaseActivity {
private boolean mIsScanKeyDown = false; //初始化控件变量
private ListView mListView;
private TextView mTextView;
private CheckBox mToneCheckBox;
private RadioGroup mRadioGroup;
private RadioButton mSingleButton;
private RadioButton mContinuousButton;
private RadioButton mKeyHoldButton;
private Button mAutoTestButton;
private Button mSingleScanButton;
private Button mContinousScanButton;
protected boolean inContinuousShoot = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mListView = (ListView) findViewById(R.id.list);
mAutoTestButton = (Button) findViewById(R.id.button_auto_test);
mToneCheckBox = (CheckBox) findViewById(R.id.checkBox_tone);
mToneCheckBox.setChecked(mSoundUtils.getPlay());
//通过这个按扭事件来设置声音是否可用
mToneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSoundUtils.enablePlay(isChecked); //单选框是保证这个是否可用,如果不可用,就不播放声音了
}
});
mSingleButton = (RadioButton) findViewById(R.id.single_radioButton); //单扫按键
mContinuousButton = (RadioButton) findViewById(R.id.continuous_radioButton); //连扫按键
mKeyHoldButton = (RadioButton) findViewById(R.id.key_radioButton);
mRadioGroup = (RadioGroup) findViewById(R.id.rg);
//单选框的点击事件
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.single_radioButton:
mScannerManager.setScanMode(ScannerManager.SCAN_SINGLE_MODE); //设置为单扫模式
break;
case R.id.continuous_radioButton: //多扫模式
mScannerManager.setScanMode(ScannerManager.SCAN_CONTINUOUS_MODE);
break;
case R.id.key_radioButton: //挂起模式
mScannerManager.setScanMode(ScannerManager.SCAN_KEY_HOLD_MODE);
break;
default:
break;
}
}
});
// mAutoCheckBox = (CheckBox) findViewById(R.id.checkBox_auto);
// mAutoCheckBox.setChecked(mAutoScan);
// mAutoCheckBox.setOnCheckedChangeListener(new
// OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean
// isChecked) {
// mAutoScan = isChecked;
// mHandle.sendEmptyMessage(AUTO_SCAN);
// }
// });
mSingleScanButton = (Button) findViewById(R.id.single_button);
mContinousScanButton = (Button) findViewById(R.id.continuous_button);
mListView.setAdapter(mListAdaper);
}
//恢复状态,用来恢复
public void onResume() {
super.onResume();
switch (mScannerManager.getScanMode()) { //获取 这个状态,如果是连扫模式,那么单扫不可用,自动测试可用
case ScannerManager.SCAN_CONTINUOUS_MODE:
mSingleScanButton.setEnabled(false);
mContinuousButton.setChecked(true);
mAutoTestButton.setEnabled(false);
break;
case ScannerManager.SCAN_KEY_HOLD_MODE: //如果是自动测试,那么前面两个不可用
mContinousScanButton.setEnabled(false);
mSingleScanButton.setEnabled(false);
mKeyHoldButton.setChecked(true);
break;
case ScannerManager.SCAN_SINGLE_MODE: //单扫模式
default:
mContinousScanButton.setEnabled(false);
mSingleButton.setChecked(true);
break;
}
}
/*
* public boolean dispatchKeyEvent(KeyEvent event) { if(event.getKeyCode()
* == KeyEvent.KEYCODE_BUTTON_A) {
* mScannerManager.dispatchScanKeyEvent(event); return true; } return
* super.dispatchKeyEvent(event);
*
* }
*/
//这两个按扭事件一个意思就是为了防止长按,设置了一个策略,如果长按,那么为false也就是不会执行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_BUTTON_A: {
if (!mIsScanKeyDown) { //如果非按下状态,
pressed++;//按下计数器增加
mScanner
/**
* Project Name:YTOInfield File Name:SoundManager.java Package
* Name:cn.net.yto.infield.biz Date:2013-3-7 am 10:10:13 Copyright (c) 2013,
* zhiliantiandi All Rights Reserved.
*/
package com.juri.juriscanner;
import com.juri.juriscanner.R;
import android.content.Context; //上下文
import android.media.AudioManager;
import android.media.SoundPool;
/**
* ClassName:SoundManager <br/>
* Date: 2013-3-7 am 10:10:13 <br/>
*
* @author Liliang
* @version
* @since JDK 1.6
* @see
*/
//加了final的话,这个类无法继承的
public final class SoundUtils {
//定义声音的三种状态,成功为0,警告为1,查询为2
public static final int SOUND_TYPE_SUCCESS = 0; //静态变量,这样就保证了只有一份
public static final int SOUND_TYPE_WARNING = 1;
public static final int SOUND_TYPE_QUERY = 2;
private static SoundUtils sManager; //实例本身,因为这个类不打算继承,那么只有只有通过实例化来猎取
private float mStreamVolume = 0.1f;
private Context mContext; //上下文菜单
private int mWarningId = R.raw.warning; //这里是资源ID
private int mSuccessId = R.raw.success;
private int mQueryId = R.raw.query;
private boolean isPlay = true; //默认为正在播放
//构造方法
private SoundUtils() {
}
//初始化,他这个奇怪的在于,写在里面了,原因是因为不想继承,只有这个办法,实例化对象
public static SoundUtils getInstance() {
if (sManager == null) {
sManager = new SoundUtils();
}
return sManager;
}
//初始化
public void init(Context context) {
if (mContext == null || mSoundPool == null) {
mContext = context;
loadSoundResources(context);
}
}
//初始化方法
public void init(Context context, int warningId, int successId, int queryId) {
if (mContext == null || mSoundPool == null) {
mContext = context;
mWarningId = warningId;
mSuccessId = successId;
mQueryId = queryId;
loadSoundResources(context);
}
}
//是否可用,默认是可用的
public void enablePlay(boolean enable) {
isPlay = enable; //赋值
}
//获取 状态
public boolean getPlay() {
return isPlay;
}
/**
* playSound:Play sound for scan. <br/>
*
* @author Liliang
* @param soundType One of {@link #SOUND_TYPE_SUCCESS},
* {@link #SOUND_TYPE_WARNING}, or {@link #SOUND_TYPE_QUERY}
* @since JDK 1.6
*/
//播放声音,参数为类型,不同的声音种类
public void playSound(int soundType) {
if (!isPlay)
return; //如果不允许播,那么直接返回
int soundResId = mSoundSuccessId; //默认是成功的
switch (soundType) {
case SOUND_TYPE_SUCCESS:
soundResId = mSoundSuccessId;
break;
case SOUND_TYPE_WARNING:
soundResId = mSoundWarningId;
break;
case SOUND_TYPE_QUERY:
soundResId = mSoundQueryId;
break;
default:
break;
}
mSoundPool.play(soundResId, getVolume(), getVolume(), 1, 0, 1f);
}
//播放其他的声音
public void playOther(int resourceId) {
if (!isPlay)
return;
int id = mSoundPool.load(mContext, resourceId, 1);
mSoundPool.play(id, getVolume(), getVolume(), 1, 0, 1f);
}
//播放警告
public void warn() {
if (!isPlay)
return;
mSoundPool.play(mSoundWarningId, getVolume(), getVolume(), 1, 0, 1f);
}
//左右分开
public void warn(float leftVolume, float rightVolume) {
if (!isPlay)
return;
mSoundPool
.play(mSoundWarningId, leftVolume, rightVolume, 1, 0, 1f);
}
//播放正确的
public void success() {
if (!isPlay)
return;
mSoundPool
.play(mSoundSuccessId, getVolume(), getVolume(), 1, 0, 1f);
}
//正确的左右
public void success(float leftVolume, float rightVolume) {
if (!isPlay)
return;
mSoundPool
.play(mSoundSuccessId, leftVolume, rightVolume, 1, 0, 1f);
}
public void query() {
if (!isPlay)
return;
mSoundPool.play(mSoundQueryId, getVolume(), getVolume(), 1, 0, 1f);
}
public void query(float leftVolume, float rightVolume) {
if (!isPlay)
return;
mSoundPool.play(mSoundQueryId, leftVolume, rightVolume, 1, 0, 1f);
}
//声音大小
private float getVolume() {
return 0.8f;
}
private void loadSoundResources(Context context) {
release();
if (mSoundPool == null) {
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mSoundWarningId = mSoundPool.load(context, mWarningId, 1);
mSoundSuccessId = mSoundPool.load(context, mSuccessId, 1);
mSoundQueryId = mSoundPool.load(context, mQueryId, 1);
}
}
//关闭资源,停止播放
public void release() {
if (mSoundPool != null) {
mSoundPool.release();
}
mSoundPool = null;
}
//定义三个自定义对象,用来播放声音
private SoundPool mSoundPool;
private int mSoundWarningId;
private int mSoundSuccessId;
private int mSoundQueryId;
}
我简单的想测试下
package com.juri.juricode;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//下面代码为段涛于2020年5月6日手工编写,今天完工,做个最好的
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.ListView;
//导入支持库
import com.zltd.decoder.Constants;
import com.zltd.decoder.DecoderManager;
import com.zltd.industry.ScannerManager;
//调用SDK接口
public class MainActivity extends AppCompatActivity implements ScannerManager.IScannerStatusListener {
//定义几个全局变量
ScannerManager mScannerManager;
TextView mTV;
private static final int UPDATE_LIST = 4096;
private static final int UPDATE_NUMBER = 4097;
public boolean isScankeyDown = false; //默认是弹起的
//定义一个Handler对象用来更新UI
protected Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what) {
case UPDATE_LIST:
mTV.setText((String) msg.obj);
default:
break;
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScannerManager = ScannerManager.getInstance();//初始化实例
mTV = findViewById(R.id.mTV);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BUTTON_A:
if (!isScankeyDown) {
mScannerManager.dispatchScanKeyEvent(event);
isScankeyDown = true; //如果不是是按下状态,那么设为按下去的
}
return true; //返回为真,就是弹起来的意思
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BUTTON_A:
if (isScankeyDown) {
isScankeyDown = false; //如果是按下状态,那么设为弹起来的
mScannerManager.dispatchScanKeyEvent(event);
}
return true; //返回为真,就是弹起来的意思
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onScannerStatusChanage(int i) {
}
@Override
public void onScannerResultChanage(byte[] arg0) {
String data = new String(arg0);
Message msg = mHandler.obtainMessage(UPDATE_LIST, data); //接收消息,发送消息
mHandler.sendMessage(msg);
}
}
为啥不起作用了
package com.juri.juriscanner;
import com.zltd.industry.ScannerManager; //调用sdk接口
import com.juri.juriscanner.R;
import android.content.Intent; //intent方法
import android.os.Bundle;
import android.view.KeyEvent; //键盘事件
import android.view.View;
import android.widget.Button; //下面这些都是控件
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends BaseActivity {
private boolean mIsScanKeyDown = false; //初始化控件变量
private ListView mListView;
private TextView mTextView;
private CheckBox mToneCheckBox;
private RadioGroup mRadioGroup;
private RadioButton mSingleButton;
private RadioButton mContinuousButton;
private RadioButton mKeyHoldButton;
private Button mAutoTestButton;
private Button mSingleScanButton;
private Button mContinousScanButton;
protected boolean inContinuousShoot = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mListView = (ListView) findViewById(R.id.list);
mAutoTestButton = (Button) findViewById(R.id.button_auto_test);
mToneCheckBox = (CheckBox) findViewById(R.id.checkBox_tone);
mToneCheckBox.setChecked(mSoundUtils.getPlay());
//通过这个按扭事件来设置声音是否可用
mToneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSoundUtils.enablePlay(isChecked); //单选框是保证这个是否可用,如果不可用,就不播放声音了
}
});
mSingleButton = (RadioButton) findViewById(R.id.single_radioButton); //单扫按键
mContinuousButton = (RadioButton) findViewById(R.id.continuous_radioButton); //连扫按键
mKeyHoldButton = (RadioButton) findViewById(R.id.key_radioButton);
mRadioGroup = (RadioGroup) findViewById(R.id.rg);
//单选框的点击事件
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.single_radioButton:
mScannerManager.setScanMode(ScannerManager.SCAN_SINGLE_MODE); //设置为单扫模式
break;
case R.id.continuous_radioButton: //多扫模式
mScannerManager.setScanMode(ScannerManager.SCAN_CONTINUOUS_MODE);
break;
case R.id.key_radioButton: //挂起模式
mScannerManager.setScanMode(ScannerManager.SCAN_KEY_HOLD_MODE);
break;
default:
break;
}
}
});
// mAutoCheckBox = (CheckBox) findViewById(R.id.checkBox_auto);
// mAutoCheckBox.setChecked(mAutoScan);
// mAutoCheckBox.setOnCheckedChangeListener(new
// OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean
// isChecked) {
// mAutoScan = isChecked;
// mHandle.sendEmptyMessage(AUTO_SCAN);
// }
// });
mSingleScanButton = (Button) findViewById(R.id.single_button);
mContinousScanButton = (Button) findViewById(R.id.continuous_button);
mListView.setAdapter(mListAdaper);
}
//恢复状态,用来恢复
public void onResume() {
super.onResume();
switch (mScannerManager.getScanMode()) { //获取 这个状态,如果是连扫模式,那么单扫不可用,自动测试可用
case ScannerManager.SCAN_CONTINUOUS_MODE:
mSingleScanButton.setEnabled(false);
mContinuousButton.setChecked(true);
mAutoTestButton.setEnabled(false);
break;
case ScannerManager.SCAN_KEY_HOLD_MODE: //如果是自动测试,那么前面两个不可用
mContinousScanButton.setEnabled(false);
mSingleScanButton.setEnabled(false);
mKeyHoldButton.setChecked(true);
break;
case ScannerManager.SCAN_SINGLE_MODE: //单扫模式
default:
mContinousScanButton.setEnabled(false);
mSingleButton.setChecked(true);
break;
}
}
/*
* public boolean dispatchKeyEvent(KeyEvent event) { if(event.getKeyCode()
* == KeyEvent.KEYCODE_BUTTON_A) {
* mScannerManager.dispatchScanKeyEvent(event); return true; } return
* super.dispatchKeyEvent(event);
*
* }
*/
//这两个按扭事件一个意思就是为了防止长按,设置了一个策略,如果长按,那么为false也就是不会执行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_BUTTON_A: {
if (!mIsScanKeyDown) { //如果非按下状态,
pressed++;//按下计数器增加
mScannerManager.dispatchScanKeyEvent(event);//这个才是主要事件,调用sdk方法,触发扫描SDK扫描事件
// mScannerManager.triggerLevel(ScannerManager.LOW_LEVEL);
mHandle.sendEmptyMessage(4097); //添加数据,添加数据的IP为4097,意思就是升级记录条数
mIsScanKeyDown = true; //设置为真,防止连续按
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_BUTTON_A: {
if (mIsScanKeyDown) { //触发按下弹起事件
mIsScanKeyDown = false;
mScannerManager.dispatchScanKeyEvent(event);
// mScannerManager.triggerLevel(ScannerManager.HIGH_LEVEL);
}
return true;
}
}
return super.onKeyUp(keyCode, event);
}
public void onClear(View view) {
clear();
}
//启动自动测试,跳到新窗口
public void onAutoTest(View view) {
Intent intent = new Intent();
intent.setClassName("com.zltd.scantest", "com.zltd.scantest.AutoTestActivity");
startActivity(intent);
}
//计数器显示在文本标签
public void updateCount() {
mTextView.setText("Scan: " + pressed + "times\nResult: " + scanned + "times");
}
//添加文本的方法
@Override
public void updateList(String data) {
//mBarcodeList.add(0, data);
//mListAdaper.notifyDataSetChanged();
mTextView.setText(data);
}
public void singleShootOnClick(View view) {
mScannerManager.singleScan();
}
//连扫事件
public void continuousShootOnClick(View view) {
if(!inContinuousShoot){
inContinuousShoot = true;
mScannerManager.startContinuousScan();
} else {
inContinuousShoot = false;
mScannerManager.stopContinuousScan();
}
}
}