srinivasa
srinivasa

Reputation: 11

How to stop the UI thread in my app until other thread completes printing

Description:

problem: The UI thread in the app is not waiting and its immediately getting the default status false

Where I am doing mistake in the following code?

UI thread:

private void printingStatus()
{
    if(printData.isPrintFinished())
        // do something
    else
        // do something
}

// PrintData class that executes printing in a different thread

    private boolean printFinished = false;
// UI thread asks this method whether printing is success or failure
public boolean isPrintFinished() {
    return printed;
}

public void setPrintFinished(boolean printed) {
    this.printed = printed;
}

public static interface IPrintFinished {
    public void onPrintFinished(boolean success);
}

public void printData() {
    sendDataToPrinter(context, contentToPrint, new IPrintFinished() {
        @Override
        public void setPrintFinished(boolean success) {
            if (success) {
                setPrintFinished(true);
                Logger.e("inside if success - printed"+printed);
            }
            setPrintFinished(false);
            Logger.e("printed outside if" + printed);
        }
    });
}
public static void sendDataToPrinter(final Context context, final contentDefinition contentToPrint, final IPrintFinished listener) {
    new Thread(new Runnable() {
    @Override
    public void run() {
        Looper.prepare();
        // starts here the actual printing and gets a result
        int tmpResult;
          try {
              DataPrinter.getInstance().print(contentToPrint);
              try {
                  Thread.sleep(2000);
              } catch (InterruptedException e) {
                  Logger.e("exceptpion:" + e);
              }
              tmpResult =  STATUS_PRINTED;
          } catch (TechnicalException e) {
              Logger.e("exceptpion:" + e);
              if (e.getMessage() != null && DataPrinter.ERROR_BLUETOOH_IS_DISABLED.equals(e.getMessage()))
                  tmpResult = STATUS_BLUETOOTH_DISABLED;
              else
                  tmpResult = STATUS_ERROR;
          }


          final int result = tmpResult;
          Logger.e("print result:"+result);
          new Handler(Looper.getMainLooper()).post(new Runnable() {

              @Override
              public void run() {

                  if(result == STATUS_PRINTED) {
                      if (listener != null) {
                          Logger.e("inside the listener status printed");
                          listener.onPrintFinished(true);
                      }
                  } else if (result == STATUS_BLUETOOTH_DISABLED) {
                      SimpleToast.showToast("bluetooth disabled");
                      if (listener != null) {
                          listener.onPrintFinished(false);
                      }
                  } else {
                      SimpleToast.showToast("cannot connect to printer");
                      if (listener != null) {
                          listener.onPrintFinished(false);
                      }
                  }
              }
          });

                Looper.loop();
            }
        }).start();
    }

Upvotes: 0

Views: 1196

Answers (1)

Shahzeb
Shahzeb

Reputation: 3734

You cannot hold your UI thread for sometime. That will result in ANR state. Its the main thread which handles UI components.

Upvotes: 2

Related Questions