Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// runs a method every 2000ms
// example runThisEvery2seconds();
}
}, 2000);
} //end of OnCreate
public void runThisEvery2seconds()
{
//code that runs every 2 seconds
Toast.makeText(getBaseContext(), "Run from timer", Toast.LENGTH_SHORT);
}
對於目前我已經嘗試過,但沒有出現Toast消息。不知道你是否被允許這樣做,但無論如何,如果我真的在runThisEvery2seconds()內執行代碼,除了吐司之外,它會每2秒運行一次嗎?Android定時器自動運行方法