Nir*_*rav 10 android android-2.2-froyo
在我的Android应用程序中,我想在常规时间间隔调用特定方法,即."每5秒钟后"......我怎么能这样做......?
Vik*_*dar 18
您可以将Timer用于方法的固定周期执行.
这是一个代码示例:
final long period = 0;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// do your task here
}
}, 0, period);
Run Code Online (Sandbox Code Playgroud)
小智 12
上面的链接经过测试,运行正常.这是每秒调用一些方法的代码.你可以随时改变1000(= 1秒)(例如3秒= 3000)
public class myActivity extends Activity {
private Timer myTimer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22308 次 |
| 最近记录: |