AsyncTask简单应用(一)

来源:互联网 发布:js调用ajax方法 编辑:程序博客网 时间:2024/05/29 18:46

Java:

package com.max.test.asynctask_download;import java.io.IOException;import javax.crypto.spec.IvParameterSpec;import org.apache.http.HttpEntity;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.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.ProgressBar;public class MainActivity extends Activity {Button button;ImageView imageView;String path = "http://c.hiphotos.baidu.com/album/w%3D2048/"+ "sign=5b108a9f6d81800a6ee58e0e850d32fa/"+ "d788d43f8794a4c2f756943b0ff41bd5ad6e39ac.jpg";ProgressDialog dialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) findViewById(R.id.button1);imageView = (ImageView) this.findViewById(R.id.imageView1);dialog = new ProgressDialog(this);dialog.setTitle("正在下载");dialog.setMessage("请稍后...");button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew MyAsyncTask().execute(path);}});}public class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {@Overrideprotected Bitmap doInBackground(String... params) {// TODO Auto-generated method stubBitmap bitmap = null;HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(params[0]);try {HttpResponse httpResponse = httpClient.execute(httpGet);HttpEntity httpEntity = httpResponse.getEntity();byte[] arrays = EntityUtils.toByteArray(httpEntity);bitmap = BitmapFactory.decodeByteArray(arrays, 0, arrays.length);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return bitmap;}@Overrideprotected void onPostExecute(Bitmap result) {// TODO Auto-generated method stubsuper.onPostExecute(result);dialog.dismiss();imageView.setImageBitmap(result);}@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();dialog.show();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


Xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="下载网络图片" />    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>