I have a very, very messed up method. I want to return ints from a handler. My code is like this:
public void getCasesFromFolder() {
msg = handler.obtainMessage();
timer.schedule(new TimerTask(){
public void run(){
if(folder.list().length == 0) {
msg.obj = "0";
handler.sendMessage(msg);
}
else {
msg.obj = Integer.toString(folder.list().length) ;
handler.sendMessage(msg);
}
}
}, delay, 3000);
}
private Handler handler = new Handler() {
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message m) {
String message = (String) m.obj;
if(message.equals("0")) {
returnValues(0);
}
else {
returnValues(Integer.parseInt(message));
}
}
public int returnValues(int i) {
if(i == 0)
return 0;
else
return i;
}
};
This doesn’t work, of course. How can I return a int from a handler?
Create an
intattribute in the class, and make your handler set its value inhandleMessage()method instead of returning it.By the way your
returnValues() method is completely useless as it can be simplified to justreturn i;which does nothing but return the parameter.