首先,感谢您花时间阅读我的问题。在 Java 编程方面,我仍然是个菜鸟,我很感激任何建议或帮助。
应用程序
我写了一个小的 android 应用程序,它通过蓝牙打印机打印出工作卡票。
问题
我的应用程序每 5 秒只能打印一张票,这意味着如果在 5 秒内捕获第二张作业卡,则该应用程序不会打印一张票。
我想要的是
我真的希望应用程序为每张捕获的工作卡连续打印一张票。
我想我迷失了 AsyncTasks、Threads (Bluetooth comms) 等的细节以及何时使用什么类。
任何关于如何使用这些类的最佳实践的指南都非常受欢迎。我在下面粘贴了一些我怀疑可能是问题根源的源代码。
设备 :
斑马 RW420 打印机(斑马 SDK)
日志内容:
03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() 在没有 BluetoothManagerCallback 的情况下调用 03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() 在没有 BluetoothManagerCallback 的情况下调用 03-06 20 :17:04.386: D/dalvikvm(25552): GC_CONCURRENT 释放 133K,9% 释放 4079K/4468K,暂停 17ms+23ms,总计
源代码 :
主屏幕.java
public class MainScreen extends Activity {
submitBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Do printing
String tpl = "Hello World!";
new PrintTask().execute(tpl);
}
});
}
//Async Printing Task
private class PrintTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
uiHelper.showLoadingDialog("Printing, please wait...");
}
@Override
protected String doInBackground(String... params) {
String msg = params[0];
print(msg);
publishProgress(1);
return "All Done!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
uiHelper.updateLoadingDialog("Printing jobcard " + values[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
uiHelper.dismissLoadingDialog();
finish(); //close the window
}
}
public void print(String tpl) {
final String msg = tpl;
//runOnUiThread(new Runnable()
new Thread(new Runnable() {
public void run() {
Looper.prepare();
printer_ob.printMessage(msg);
Looper.loop();
Looper.myLooper().quit();
}
}).start();
}
}
我使用的 ZebraPrinter 助手类:
public class ZebraPrinterHelper {
Context context;
private ZebraPrinterConnection zebraPrinterConnection;
private RadioButton btRadioButton;
private ZebraPrinter printer;
private TextView statusField;
private EditText macAddress, ipDNSAddress, portNumber;
private Button testButton;
public ZebraPrinterHelper(Context c) {
context = c;
}
public ZebraPrinter connect() {
zebraPrinterConnection = null;
zebraPrinterConnection = new BluetoothPrinterConnection(SettingsHelper.getBluetoothAddress(context));
try {
zebraPrinterConnection.open();
} catch (ZebraPrinterConnectionException e) {
Toast.makeText(context, "Comm Error! Disconnecting", 500).show();
DemoSleeper.sleep(1000);
disconnect();
}
ZebraPrinter printer = null;
if (zebraPrinterConnection.isConnected()) {
try {
printer = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
//setStatus("Determining Printer Language", Color.YELLOW);
PrinterLanguage pl = printer.getPrinterControlLanguage();
// setStatus("Printer Language " + pl, Color.BLUE);
} catch (ZebraPrinterConnectionException e) {
//"Unknown Printer Language");
Toast.makeText(context, "Error, Unknown printer language", 500).show();
DemoSleeper.sleep(1000);
printer = null;
DemoSleeper.sleep(1000);
disconnect();
} catch (ZebraPrinterLanguageUnknownException e) {
//setStatus("Unknown Printer Language", Color.RED);
Toast.makeText(context, "Error, Unknown printer language", 500).show();
printer = null;
DemoSleeper.sleep(1000);
disconnect();
}
}
return printer;
}
private void writeMessage(byte[] message) {
//message in bytes
try {
zebraPrinterConnection.write(message);
DemoSleeper.sleep(1500);
if (zebraPrinterConnection instanceof BluetoothPrinterConnection) {
String friendlyName = ((BluetoothPrinterConnection) zebraPrinterConnection).getFriendlyName();
DemoSleeper.sleep(500);
}
} catch (ZebraPrinterConnectionException e) {
//helper.showErrorDialogOnGuiThread("Error:" + e.getMessage());
Log.d("Error",e.getMessage());
} finally {
disconnect();
}
}
public void printMessage(String message) {
byte[] msg = null;
msg = message.getBytes();
//check connections
printer = connect();
if (printer != null) {
writeMessage(msg);
} else {
disconnect();
}
}
public void disconnect() {
try {
if (zebraPrinterConnection != null) {
zebraPrinterConnection.close();
}
} catch (ZebraPrinterConnectionException e) {
//setStatus("COMM Error! Disconnected", Color.RED);
} finally {
}
}
}