Jos*_*lar 24 javascript java android equivalent
我需要setTimeOut(call function(),milliseconds);android 的等效代码.
setTimeOut(call function(),milliseconds);
Run Code Online (Sandbox Code Playgroud)
Mat*_*lfe 21
您可能想查看TimerTask
既然你再次提出这个问题,我想提出一个不同的建议,即Handler.它比TimerTask更简单,因为你不需要明确地调用runOnUiThread,因为Handler将与UI线程相关联,只要它在UI线程上创建,或者你使用它的构造函数中的主循环器创建它.它会像这样工作:
private Handler mHandler;
Runnable myTask = new Runnable() {
@Override
public void run() {
//do work
mHandler.postDelayed(this, 1000);
}
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
mHandler = new Handler(Looper.getMainLooper());
}
//just as an example, we'll start the task when the activity is started
@Override
public void onStart() {
super.onStart();
mHandler.postDelayed(myTask, 1000);
}
//at some point in your program you will probably want the handler to stop (in onStop is a good place)
@Override
public void onStop() {
super.onStop();
mHandler.removeCallbacks(myTask);
}
Run Code Online (Sandbox Code Playgroud)
在您的活动中,处理程序需要注意一些事项:
Gia*_*bot 11
这是我在当前项目中使用的代码.Matt说,我使用了TimerTask.60000是milisec.= 60秒 我用它来刷新比赛分数.
private void refreshTimer() {
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
adapter = Score.getScoreListAdapter(getApplicationContext());
adapter.forceReload();
setListAdapter(adapter);
}
});
}
}, 0, 60000);
Run Code Online (Sandbox Code Playgroud)