Priority thread execution android

2

I have the following code to print to P.O.S on Android; Before I call up the data for thermal printing, I'll call to test if the printer has paper or everything is fine with it.

PrinterDemo.getInstance(getApplicationContext(),toast,dialog).testeImpressao();

The content of the testImpression method is as follows:

public void testeImpressao() throws RemoteException {
        Printer.getInstance().getStatus();

        Printer.getInstance().start(new OnPrintListener.Stub() {

            @Override
            public void onFinish() throws RemoteException {
                Log.d(TAG, "----- onFinish -----");

                SendJsonPayment.eita = "deucerto";
                //hideDialog();
                //showToast(R.string.succeed);
            }

            @Override
            public void onError(int error) throws RemoteException {
                Log.d(TAG, "----- onError ----");
                SendJsonPayment.eita = "deuerrado";
                //hideDialog();
                //showToast(Printer.getErrorId(error));
            }
        });
    }

The problem is that I need to know if everything is okay before sending the print data but soon after that line "Printer.getInstance().start(new OnPrintListener.Stub()" it exits the block and returns to the subsequent line of the test method call, which is where I test if worked out or not:

if (eita.equals("deucerto"))

And then it comes back and falls into the onError method, that is, my condition is never satisfied, I would like to know how I can execute it immediately and introduce myself before the test happens.

    
asked by anonymous 17.09.2018 / 16:41

2 answers

1

By all means the start() method is asynchronous, that is, it delegates execution to a relatively long portion of code in another thread and when it finishes executing that thread it calls onFinish() or onError() .

So it does not make much sense to have code after start() , where you say the if (eita.equals("deucerto")) is checked, because the start() call is only delegating execution to another part of the code and immediately after it is not for it does not matter that you should wait for the processing result.

The code that is within this if should be moved to the onFinish() method, which is when the "deucerto" condition occurs.

Similarly, if there is a check for "não deu certo" (for example a else ), the corresponding code block must be moved into the onError() method.

I hope you have understood.

What are asynchronous processing and synchronous processing?

    
17.09.2018 / 19:44
1

Just like @Piovezan answered the start method is asynchronous, so I suggest a callback, a callback is used to "call it back" when a certain asynchronous action is terminated

Let's create a callback, you can do it this way, or customize it as you prefer:

public interface ICallback{
      public void onSucesso();
      public void onErro(String mensagem, int codigoErro);
}

Now, you will check if the operation was successful using the callback, so you need to receive the callback per parameter:

public void testeImpressao(final ICallback quandoConseguirUmResultado) throws RemoteException {
    Printer.getInstance().getStatus();

    Printer.getInstance().start(new OnPrintListener.Stub() {

        @Override
        public void onFinish() throws RemoteException {
            Log.d(TAG, "----- onFinish -----");

            SendJsonPayment.eita = "deucerto";
            //hideDialog();
            //showToast(R.string.succeed);
            quandoConseguirUmResultado.onSucesso();
        }

        @Override
        public void onError(int error) throws RemoteException {
            Log.d(TAG, "----- onError ----");
            SendJsonPayment.eita = "deuerrado";
            //hideDialog();
            //showToast(Printer.getErrorId(error));

            quandoConseguirUmResultado.onErro("Ocorreu um erro", error);
        }
    });
}

Now you need to pass the callback per parameter:

PrinterDemo.getInstance(getApplicationContext(),toast,dialog).testeImpressao(new ICallback() {
        @Override
        public void onSucesso() {
              //operação realizada com sucesso
        }

        @Override
        public void onErro(String erro, Integer codigo) {
              //continue aqui para tratar o erro
        }
});
    
20.09.2018 / 21:55