0
    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);
}

For the time being I have tried this but the Toast message doesn't appear. Don't know if you're allowed to do that but anyway in general, if I actually execute code inside runThisEvery2seconds() , other than Toast, will it run every 2 seconds ?

4

4 に答える 4

1

show()トースト メッセージを作成するときは、必ず電話してください。

Toast.makeText(getBaseContext(), "Run from timer", Toast.LENGTH_SHORT).show();

いいえ、メッセージは 2 秒ごとに表示されるわけではありません。 postDelayed指定された遅延の後にタスクを 1 回実行しますが、その後は完了します。スケジュールに従ってタスクを実行したい場合は、Java のTimerまたはScheduledExecutorServiceを見てください。

于 2012-05-11T13:22:23.023 に答える
1

を表示していませんToast
メソッドを呼び出しshowます。

Toast.makeText(getBaseContext(), "Run from timer", Toast.LENGTH_SHORT).show();
于 2012-05-11T13:22:55.470 に答える
1

.show()乾杯の終わり。

Toast.makeText(getBaseContext(), "タイマーから実行", Toast.LENGTH_SHORT).show();

于 2012-05-11T13:25:53.877 に答える