java - 我的 android 应用程序每 5 秒只打印一张票

标签 java android printing bluetooth zebra-printers

首先,感谢您花时间阅读我的问题。 在使用 Java 进行编程方面,我仍然是一个菜鸟,我非常感谢您提供的任何建议或帮助。

应用

我编写了一个小型 android 应用程序,可通过蓝牙打印机打印出工作卡。

问题

我的应用程序只能每 5 秒打印一张票,这意味着如果在 5 秒内捕获第二张工作卡,应用程序不会打印票。

我想要什么

我真的希望应用程序为捕获的每个工作卡连续打印出一张票。

我想我迷失了 AsyncTasks、Threads(蓝牙通信)等的细节以及何时使用什么类。

我们非常欢迎任何关于如何使用这些类的最佳实践指南。 我在下面粘贴了一些我怀疑可能是问题根源的源代码。

设备:

Zebra RW420 Printer (Zebra SDK)

日志内容:

03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() called with no BluetoothManagerCallback 03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() called with no BluetoothManagerCallback 03-06 20:17:04.386: D/dalvikvm(25552): GC_CONCURRENT freed 133K, 9% free 4079K/4468K, paused 17ms+23ms, total

源代码:

主屏幕.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 处理异步请求,并且还具有用于排队的内置功能。看看this有关 IntentServices 的更多信息的链接。

关于java - 我的 android 应用程序每 5 秒只打印一张票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15255543/

相关文章:

java - 如何在 Java 中打印已经设计好的 A4 纸上的值?

javascript - CSS 媒体打印不起作用

java - 如何将存储为小端字节数组的 uint16 转换为 int?

java - AlarmManager 第二次过早触发 PendingIntent

java - Proguard 不会混淆 Android 应用程序

android - 如何检查 Robotium 测试中剪贴板中是否存在文本?

java - 搜索用于将 GUI 组件写入 PDF 的 Java 库或第 3 方库

java - 如何将 JTable 列设置为 String 并按 Double 排序?

java - 用于小型独立应用程序的 64 位 Java

java - DDD/通过存储库一次保存多个实体的良好做法