【问题标题】:My android application prints only 1 ticket every 5 seconds我的 android 应用程序每 5 秒只打印 1 张票
【发布时间】:2013-03-06 18:49:41
【问题描述】:

首先,感谢您抽出宝贵时间阅读我的问题。 在 Java 编程方面,我仍然是个菜鸟,我很感激任何建议或帮助。

应用程序

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

问题

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

我想要什么

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

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

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

设备:

斑马 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,总计

源代码:

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 {

            }
        }
    }

【问题讨论】:

    标签: java android printing bluetooth zebra-printers


    【解决方案1】:

    我认为您将不得不使用IntentService 而不是AsyncTask。 IntentServices 处理异步请求,并且还具有用于排队的内置功能。请查看this 链接以获取有关 IntentServices 的更多信息。

    【讨论】:

    • 谢谢,我去看看。
    【解决方案2】:

    在整个代码中都有一堆DemoSleeper.sleep() 调用,听起来它会在一段时间内阻塞执行。它在open() 之后休眠 1 秒,然后在write() 之后休眠几秒钟,与打印机的通信肯定需要一些时间。

    这是导致打印缓慢的原因吗?你能试着去掉那些吗?打印时不需要它们

    【讨论】:

    • ahhh 是的,这不是我的代码,它是斑马 SDK 的一部分。我将删除它们,看看打印速度是否有所提高。好地方!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多