用ThreadHandle能够实现多线程,而后再主线程更新UIhtml
第二种就是用 android
具体看代码多线程
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); } }
主要是在另一个线程工做的函数式doInBackground(),这个函数返回的值就是onPostExcute里面的参数ide
具体看以下官方文档函数
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:ui
doInBackground()
executes automatically on a worker threadonPreExecute()
, onPostExecute()
, and onProgressUpdate()
are all invoked on the UI threaddoInBackground()
is sent to onPostExecute()
publishProgress()
at anytime in doInBackground()
to execute onProgressUpdate()
on the UI thread