首先,感谢您抽出时间阅读我的问题。 在Java编程方面,我仍然是一个菜鸟,我很感激任何建议或帮助。
我写了一个小型的Android应用程序,通过蓝牙打印机打印出作业卡票。
我的应用程序每5秒只能打印一张票,这意味着如果在5秒内捕获到第二张作业卡,则应用程序不会打印票证。
我真的希望应用程序连续打印出每张被捕获的工作卡的票据。
我认为我在AsyncTasks,Threads(蓝牙通信)等方面迷失了细节,何时使用哪个类。
欢迎使用有关如何使用这些类的最佳实践的任何指南。 我已粘贴下面的一些源代码,我怀疑它可能是问题的根源。
Zebra RW420打印机(Zebra SDK)
03-06 20:17:04.328:W / BluetoothAdapter(25552):getBluetoothService() 没有BluetoothManagerCallback调用03-06 20:17:04.328: W / BluetoothAdapter(25552):getBluetoothService()调用no BluetoothManagerCallback 03-06 20:17:04.386:D / dalvikvm(25552): GC_CONCURRENT释放133K,9%免费4079K / 4468K,暂停17ms + 23ms,总计
MainScreen.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 {
}
}
}
答案 0 :(得分:1)
我认为您必须使用IntentService
而不是AsyncTask
。 IntentServices处理异步请求,并具有用于排队的内置功能。有关IntentServices的更多信息,请查看this链接。
答案 1 :(得分:0)
你的整个代码中都有一堆DemoSleeper.sleep()
个调用,听起来好像会在一段时间内阻止执行。它在open()
之后休眠1秒,然后在write()
之后休息几秒钟,并且与打印机的通信肯定需要一些时间。
这是导致打印速度慢的原因吗?你可以尝试删除那些吗?您不需要它们进行打印