我希望从服务器同步加载数据,并将其显示在ListView或TextView之类的视图中,而不需要每次都有任何拉取请求。我需要的数据将加载,如果有任何变化在服务器上。
现在,我正在使用asynctask从服务器拉取数据,但要加载新数据或更新,我需要使用重新加载按钮进行拉取请求。
我尝试使用一个线程,该线程将在特定时间后运行。但我知道显然可能有更好的方法。
如果没有手动拉取请求,我如何做同样的事情?
例如,gmail应用程序在有网络访问时加载新的电子邮件,而不需要用户的任何拉取请求。我需要在我的Android应用程序中做同样的事情。
提前谢谢。
发布于 2016-09-15 04:03:27
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}AsyncTask确实在单独的线程上运行,但不能从UI thread以外的其他线程启动。处理程序就是允许这样做的。
https://stackoverflow.com/questions/39498403
复制相似问题