[英]Ads Changing for every 3-5 seconds in an activity of Android Application
[英]My android application prints only 1 ticket every 5 seconds
首先,感谢您抽出时间阅读我的问题。 在Java编程方面,我仍然是一个菜鸟,我很感激任何建议或帮助。
我写了一个小的Android应用程序,通过蓝牙打印机打印出作业卡票。
我的应用程序每5秒只能打印一张票,这意味着如果在5秒内捕获到第二张作业卡,则应用程序不会打印票证。
我真的希望应用程序连续打印出每个捕获的作业卡的票证。
我觉得我迷失在AsyncTasks,Threads(蓝牙通信)等细节中,以及何时使用什么类。
任何有关如何使用这些类的最佳实践的指南都非常受欢迎。 我已粘贴下面的一些源代码,我怀疑它可能是问题的根源。
Zebra RW420打印机(Zebra SDK)
03-06 20:17:04.328:W / BluetoothAdapter(25552):没有BluetoothManagerCallback调用的getBluetoothService()03-06 20:17:04.328:W / BluetoothAdapter(25552):没有BluetoothManagerCallback调用的getBluetoothService()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 {
}
}
}
我认为你将不得不使用IntentService
而不是AsyncTask
。 IntentServices处理异步请求,并具有用于排队的内置功能。 有关IntentServices的更多信息,请查看此链接。
你在整个代码中都有一堆DemoSleeper.sleep()
调用,听起来它会在一段时间内阻止执行。 它在open()
之后休眠1秒,然后在write()
之后write()
几秒钟,并且与打印机的通信肯定需要一些时间。
这是什么导致打印速度慢? 你可以尝试删除那些吗? 你不需要它们来打印
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.