Astora Astora - 3 months ago 11
Android Question

Returning value from thread Java/ Android

I have the following thread in my android class. How can I get the value of err from the thread???

public int method(){
new Thread(new Runnable() {
int err;

@Override
public void run() {
err = device.verify(30, 5, coderChoice, detectModeChoice,
0, listSearch, callbackCmd, MTFPSD.this, matchingScore);
updateView("Finger Captured Successfully", err);

}
}).start();

return err;
}


I want the value to be the return value of the method but for the life of me I can't get the value...

Answer

You have two ways of achieving this.

  1. The bad way. Create a mutable object like a list of integers and let the Thread (Runnable) write into the list. You can access the value in the list in the outer class / method.

  2. Use a Callable instead of a Runnable . A Callable can return values