Found someone has similar issue online here.
This doesn\'t work:
Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run()
Using Timer starts a new thread, I suppose that thread does not have access to getApplicationContext
. The proper way to do it is to use Handler
and call the postDelayed
method of the Handler - which does not start a new thread.
Read about this: http://developer.android.com/resources/articles/timed-ui-updates.html
The link you posted has a working example, which is the proper way to do it:
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);