andriod多線程

用ThreadHandle能夠實現多線程,而後再主線程更新UIhtml

第二種就是用 android

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裏面的參數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

相關文章
相關標籤/搜索