在我的android应用程序中,我想要在特定时间间隔即时调用特定的方法。 “每5秒钟后”......我该怎么做......?在规律的时间间隔后调用特定方法
6
A
回答
13
您可以使用Timer进行方法的固定期间执行。
下面是代码的示例:
final long period = 0;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// do your task here
}
}, 0, period);
10
以上此链接被测试和工作正常。这是每秒钟调用某种方法的代码。您可以随时更改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
}
};
}
+0
谢谢。适用于我。 – zwarrior 2016-12-22 06:36:37
相关问题
- 1. 特定时间后调用方法
- 2. 一定的随机时间间隔后的调用方法
- 3. 如何在mvc4的特定间隔后重新调用方法
- 4. 在特定的时间间隔后调用javascript函数
- 5. 定时器间隔调用长方法
- 6. Cocos2d:麻烦调度在特定时间间隔多次调用一个方法
- 7. iOS:在特定时间调用方法
- 8. 在特定时间调用方法
- 9. 在特定时间调用方法
- 10. 在特定时间调用方法
- 11. C++ 11计时库 - 如何在特定的时间间隔后执行方法?
- 12. 在每个特定时间间隔后调度r函数
- 13. 数独时间不规律
- 14. 随机时间间隔后的调用方法
- 15. 查找是否有规律的经常性时间间隔重叠的算法?
- 16. 以规律/指定的时间间隔将文件复制到目录
- 17. 在特定的时间间隔后执行任务的正确方法
- 18. 在iOS中调用特定时间间隔的动作
- 19. 在特定的时间间隔调用实现意图
- 20. 如何在特定时间后调用repaint()方法
- 21. Android:如何在特定时间后调用某种方法?
- 22. 在特定的时间间隔
- 23. 在特定的时间间隔
- 24. linux C++如何在特定的时间间隔后调用函数
- 25. 在指定的时间间隔后第一次调用函数
- 26. 两次方法调用之间的时间间隔
- 27. 在3秒钟的时间间隔后反复调用方法在背景
- 28. 在类之间调用特定方法
- 29. 在时间间隔后过期特定的托管bean实例
- 30. 在特定的时间间隔后运行instagram代码
嘿兄弟......谢谢.... – Nirav 2011-01-06 07:11:38