Priority running android thread

Asked

Viewed 104 times

2

I have the following code for printing in P.O.S on Android; Before calling pass the data to thermal printing, I make the call to test if the printer has paper or is all right with it.

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

The content of the testing 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 all right before sending the printout data but right after that line "Printer.getInstance().start(new OnPrintListener.Stub()" it leaves the block and goes back to the subsequent line of the test method call which is where I perform the test whether it worked or not :

if (eita.equals("deucerto"))

And then you come back and fall into the method onError that is my condition is never satisfied , I would like to know how I make it to execute soon and present me the error before the test happens.

2 answers

1

Just as @Piovezan answered the start method is asynchronous, so I suggest a callback, a callback serves for "call it back" (call it back) when a certain asynchronous action has finished

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 has succeeded through the use of callback, so it is necessary to receive the callback by 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 by 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
        }
});

1

It seems the method start() is asynchronous, i.e., it delegates execution to a relatively time-consuming portion of code on another thread and when it finishes executing that thread calls onFinish() or onError().

So it doesn’t make much sense to have code after the start(), where you say you’re checking if (eita.equals("deucerto")), for the call start() is just delegating the execution to another part of the code and immediately after it is not to come anything important that should wait for the result of processing.

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

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

I hope you’ve made your point.

What are asynchronous processing and synchronous processing?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.