星期一, 4月 13, 2009

Can't make a toast from TimerTask while using Timer?

1lI can't find a way to make a toast from TimerTask while using Timer. I still don't know why.

Example:
Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
}
}, 5000);

However, I did find out a solution. It works well.

final Context ctx = this;
Handler mHandler = new Handler();

Runnable
makeToast = new Runnable() {
public void run() {
Toast.makeText(ctx,
"msg", Toast.LENGTH_LONG).show();
}
};
mHandler.postDelayed(makeToast, 1000);

沒有留言: