標籤:android學習筆記之asynctas asynctask 非同步 下載
(1)該檔案下載主要示範非同步任務下載圖片
(2)布局代碼如下:一個ImageView用於存放下載的圖片,Button用於開始非同步任務下載
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="141dp" android:layout_marginTop="135dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/imageView1" android:layout_marginBottom="80dp" android:text="網頁下載圖片" /></RelativeLayout>
(3)控制類
package com.example.asynctask_download;import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {private Button button1;private ImageView imageView1;private final String IMAGE_PATH = "http://www.baidu.com/img/bd_logo1.png";// 設定網頁圖片的地址private ProgressDialog progressDialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) this.findViewById(R.id.button1);imageView1 = (ImageView) this.findViewById(R.id.imageView1);/* * 下載進度條的設定 */progressDialog = new ProgressDialog(this);progressDialog.setTitle("提示");progressDialog.setCancelable(false);progressDialog.setMessage("正在下載圖片,請耐心等候!");button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {/* * 非同步任務、執行下載圖片 */new MyTask().execute(IMAGE_PATH);}});}/* * 非同步任務:三個參數、四個步驟onPreExecute()、 * doInBackground()、onProgressUpdate()、onPostExecute()、沒有參數傳傳進去的時候可以使用void */public class MyTask extends AsyncTask<String, Integer, byte[]> {/* * onPreExecute()方法在後台操作開始前運行在UI線程上 */@Overrideprotected void onPreExecute() {super.onPreExecute();progressDialog.show();}/* * doInBackground方法運行在後台並處理後台操作 */@Overrideprotected byte[] doInBackground(String... params) {HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(params[0]);byte[] result = null;try {HttpResponse httpResponse = httpClient.execute(httpGet);if (httpResponse.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toByteArray(httpResponse.getEntity());}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}return result;}@Overrideprotected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);}/* * 一旦後台操作完畢,onPostExecute()方法就會在UI線程中運行 */@Overrideprotected void onPostExecute(byte[] result) {super.onPostExecute(result);/* * 將下載的bitmap放置在imagView中去 * result就是下載的圖片 */Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0,result.length);imageView1.setImageBitmap(bitmap);progressDialog.dismiss();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}
結果:
Android學習筆記之AsyncTask實現檔案下載任務