用ThreadHandle可以实现多线程,然后再主线程更新UI

第二种就是用 

AsyncTask

具体看代码

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }
    
    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}
View Code

主要是在另外一个线程工作的函数式doInBackground(),这个函数返回的值就是onPostExcute里面的参数

具体看如下官方文档

You should read the AsyncTask reference for a full understanding on how to use this class, but here is a quick overview of how it works:

posted on 2013-08-16 18:15  juncent  阅读(240)  评论(0编辑  收藏  举报