I have a button that calls function beginMemoryQuest(View v)
. I was expecting a count-up of numbers of 0-5 with a difference of 1 second in each. Meaning, the Handler
mHandler
should be receiving index
from 0-5. But, as it seems, the LogCat is printing that it is receiving all index=6
calls. How do I fix this? I want the Handler
to receive calls from 0-6, each with a period of a second.
I appreciate any help, thank you very much!
private static int index;
public void beginMemoryQuest(View v){
Timer t=new Timer();
for(index=0;index<6;index++){
t.schedule(new TimerTask() {
@Override
public void run() {
Bundle b=new Bundle();
b.putInt("index", index);
Message msg=new Message();
msg.setData(b);
mHandler.sendMessage(msg);
}
}, 1000*index+1);
}
}
public Handler mHandler=new Handler(){
public void handleMessage(Message msg){
int ind=msg.getData().getInt("index", -1);
Log.e("###","handling "+ind);
}
};
PS: the Handler
is has a warning: This Handler class should be static or leaks might occur