本文介绍了Android的异步,处理程序或定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每5秒,我想打电话给我的web服务,并得到文本(而不是图像),然后在我的ImageAdapter显示。什么是实现这一目标的最佳方式是什么?

Every 5 seconds, I want to call my webservice and get text (not images), then display it in my ImageAdapter. What would be the best way to accomplish this?

推荐答案

这取决于如果你想使用一个不同的线程与否。你希望用户能够与UI线程的应用程序交互,而图像下载?如果是这样,那么我肯定会使用 AsyncTask的用小进度(风格=@安卓风格/小工具.ProgressBar.Small

It depends if you want to use a different thread or not. Do you want the user to be able to interact with the application on the UI Thread while the images are downloading? If so, then I would definitely use an AsyncTask with a small ProgressBar (style="@android:style/Widget.ProgressBar.Small")

如果你不关心线程那么什么@inazaruk说。

If you don't care about threading then what @inazaruk said.

编辑:的事实是,从Web服务中检索数据最现代的应用程序将使用的AsyncTask有一个不显眼的小装载在角落里只是为了让用户知道它的更新

the truth is most modern apps that retrieve data from a web service will use an AsyncTask with a discreet little loader in the corner just to let the user know it's updating.

编辑2:这里使用的TimerTask运行的东西每5秒的一个例子。关键是 runOnUiThread()可能有更好的方法来把所有的元素放在一起但是这准确地描绘所有的部分。

Edit 2: here's an example of using a TimerTask to run something every 5 seconds. The key is the runOnUiThread(). There may be better ways to tie all the elements together but this accurately portrays all the pieces.

myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            CallWebService();
        }

    }, 0, 1000);
}

private void CallWebService()
{
    this.runOnUiThread(fetchData);
}

private Runnable fetchData = new Runnable() {
    public void run() {
      asyncTask.execute();
    }
};

这篇关于Android的异步,处理程序或定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:03