我想每隔 5 分钟重新加载一次活动的数据,我的活动将自动重新加载。但我无法做到这一点。请帮助我如何做到这一点。?
请提供一些示例代码的帮助。
如果您想要更新数据,那么您可以为您想要的时间段创建一个 TimerTask。在此任务中,您可以更改数据。
http://android.okhelp.cz/timer-simple-timertask-java-android-example/
package cz.okhelp.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimerActivity extends Activity {
TextView hTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
// public void schedule (TimerTask task, long delay, long period)
// Schedule a task for repeated fixed-delay execution after a specific delay.
//
// Parameters
// task the task to schedule.
// delay amount of time in milliseconds before first execution.
// period amount of time in milliseconds between subsequent executions.
myTimer.schedule(myTask, 3000, 1500);
}
class MyTimerTask extends TimerTask {
public void run() {
// ERROR
hTextView.setText("Impossible");
// how update TextView in link below
// http://android.okhelp.cz/timer-task-timertask-run-cancel-android- example/
System.out.println("");
}
}
}