AlertDialog无法在线程Android中工作

时间:2014-10-08 23:34:28

标签: java android multithreading bluetooth

当我尝试打印到蓝牙打印机时,我在主线程上遇到很多错误。我试图改变它以使用一个线程,但后来我的AlterDialog不起作用。 有没有其他方法可以让它在我可以显示AlterDialog的地方工作? 感谢。

public class PrinterFunctions{

private static void sendCommand(final Context context,final String portName, final String portSettings, final ArrayList<Byte> byteList) {

    new Thread() {
        @Override
        public void run() {
            StarIOPort port = null;

            try {

                port = StarIOPort.getPort(portName, portSettings, 10000, context);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }


                StarPrinterStatus status = port.beginCheckedBlock();

                if (true == status.offline) {
                    throw new StarIOPortException("A printer is offline");
                }

                byte[] commandToSendToPrinter = convertFromListByteArrayTobyteArray(byteList);
                port.writePort(commandToSendToPrinter, 0,
                        commandToSendToPrinter.length);

                port.setEndCheckedBlockTimeoutMillis(30000);
                status = port.endCheckedBlock();

                if (true == status.coverOpen) {
                    throw new StarIOPortException("Printer cover is open");
                } else if (true == status.receiptPaperEmpty) {
                    throw new StarIOPortException("Receipt paper is empty");
                } else if (true == status.offline) {
                    throw new StarIOPortException("Printer is offline");
                }
            } catch (StarIOPortException e) {
                Builder dialog = new AlertDialog.Builder(context);
                dialog.setNegativeButton("Ok", null);
                AlertDialog alert = dialog.create();
                alert.setTitle("Failure");
                alert.setMessage(e.getMessage());
                alert.setCancelable(false);
                alert.show();
            } finally {
                if (port != null) {
                    try {
                        StarIOPort.releasePort(port);
                    } catch (StarIOPortException e) {
                    }
                }
            }
        }
    }
  }.start();

}
}

3 个答案:

答案 0 :(得分:0)

如果要更新其他线程(非主线程)中的UI,则必须使用Handler

private Handler handler=new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new Thread() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Builder dialog = new AlertDialog.Builder(context);
                    dialog.setNegativeButton("Ok", null);
                    AlertDialog alert = dialog.create();
                    alert.setTitle("Failure");
                    alert.setMessage(e.getMessage());
                    alert.setCancelable(false);
                    alert.show();
                }
            });
        };
    }.start();
}

答案 1 :(得分:0)

尝试在异常捕手中使用以下代码:

    ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Builder dialog = new AlertDialog.Builder(context);
                dialog.setNegativeButton("Ok", null);
                AlertDialog alert = dialog.create();
                alert.setTitle("Failure");
                alert.setMessage(e.getMessage());
                alert.setCancelable(false);
                alert.show();
            }
        });

OR

 final Handler handler = new Handler();
        new Thread() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Builder dialog = new AlertDialog.Builder(context);
                      dialog.setNegativeButton("Ok", null);
                      AlertDialog alert = dialog.create();
                      alert.setTitle("Failure");
                      alert.setMessage(e.getMessage());
                      alert.setCancelable(false);
                      alert.show();
                    }
                });
            };
        }.start();

答案 2 :(得分:0)

我知道这是一篇过时的文章,但我只想帮助那些会在这里结束的人。

我将为现有线程内的警报对话框创建一个新线程吗?

从技术上讲,您不是在创建新线程,而是calling主线程来创建警报对话框,因为您当前位于后台线程中。

因此,要弹出对话框,需要Main线程执行警报对话框命令。

尝试替换

                dialog.setNegativeButton("Ok", null);
                AlertDialog alert = dialog.create();
                alert.setTitle("Failure");
                alert.setMessage(e.getMessage());
                alert.setCancelable(false);
                alert.show();

使用

runOnUiThread(new Runnable() 
{
    @Override
    public void run() 
    {
        Builder dialog = new AlertDialog.Builder(context);
                dialog.setNegativeButton("Ok", null);
                AlertDialog alert = dialog.create();
                alert.setTitle("Failure");
                alert.setMessage(e.getMessage());
                alert.setCancelable(false);
                alert.show();
    });

或者您可以使用处理程序

protected Handler handler = new Handler(Loop.getMainLooper)

handler.post(new Runnable() 
{
    @Override
    public void run() 
    {
        Builder dialog = new AlertDialog.Builder(context);
                dialog.setNegativeButton("Ok", null);
                AlertDialog alert = dialog.create();
                alert.setTitle("Failure");
                alert.setMessage(e.getMessage());
                alert.setCancelable(false);
                alert.show();
    });