AsyncTask定義了三種泛型類型 Params,Progress和Result。
?Params 啟動任務執行的輸入參數,比如HTTP請求的URL。
?Progress 後台任務執行的百分比。
?Result 後台執行任務最終返回的結果,比如String。
AsyncTask定義了三種泛型類型 Params,Progress和Result。
?Params 啟動任務執行的輸入參數,比如HTTP請求的URL。
?Progress 後台任務執行的百分比。
?Result 後台執行任務最終返回的結果,比如String。view plaincopy to clipboardprint?
一切說明都在代碼中......................................................
Activity
- /*
- * Android 異步任務AsyncTask
- * AsyncTask定義了三種泛型類,Params,Progress,Result
- */
- package rw.ansync;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
-
- public class AsynaTask_TestActivity extends Activity {
- /** Called when the activity is first created. */
- private EditText editText;
- private Button button;
- private ProgressBar progressBar;
- private ImageView imageView;
- private static final String URL_STRING="http://photocdn.sohu.com/20110927/Img320705637.jpg";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- editText=(EditText) findViewById(R.id.editText1);
- editText.setText(URL_STRING);
- button=(Button) findViewById(R.id.button1);
- progressBar=(ProgressBar) findViewById(R.id.progressBar1);
- imageView=(ImageView) findViewById(R.id.imageView1);
- button.setOnClickListener(new ButtonListener());
- }
- class ButtonListener implements OnClickListener{
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- MyAsynaTask mTask=new MyAsynaTask(imageView, progressBar,button);
- mTask.execute(URL_STRING);
- }
-
- }
-
-
- }
MyAsynaTask.java
- package rw.ansync;
-
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
-
-
- import android.R.integer;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
-
-
- public class MyAsynaTask extends AsyncTask<String, Integer, Bitmap>{
-
- private ImageView imageView;
- private ProgressBar progressBar;
- private Button button;
-
- public MyAsynaTask( ImageView imageView,
- ProgressBar progressBar,Button button) {
- super();
- this.imageView = imageView;
- this.progressBar = progressBar;
- this.button=button;
- }
-
-
- @Override
- protected void onPreExecute() {
- // TODO Auto-generated method stub
-
- /*
- * 改方法在執行實際的後台操作時被UI線程調用,可以在該方法中做一些准備工作,比如
- * Toast.makeText(context, "准備下載", Toast.LENGTH_LONG).show();
- */
- super.onPreExecute();
- }
-
- @Override
- protected Bitmap doInBackground(String... params) {//輸入編變長的可變參數 和UI線程中的Asyna.execute()對應
- // TODO Auto-generated method stub
- /*
- * 該方法在OnpreExecute執行以後馬上執行,改方法執行在後台線程當中,負責耗時的計算,可以調用publishProcess方法來實時更新任務進度
- */
- Bitmap bitmap=null;
- try {
- URL url=new URL(params[0]);
- HttpURLConnection connection=(HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.connect();
- progressBar.setMax(connection.getContentLength());
-
- InputStream inputStream=connection.getInputStream();
- bitmap=BitmapFactory.decodeStream(inputStream);
- // inputStream.close();
- /*
- * 以下完全為了演示進度條,如果為了顯示進度條可以把這個去掉
- */
- /*
- byte []buf=new byte[1024*4];
- inputStream=connection.getInputStream();
- int len=0;
- while ((inputStream.read(buf))!=-1) {
- len+=buf.length;
- publishProgress(len);
- }
- */
-
- inputStream.close();
-
- } catch (Exception e) {
- // TODO: handle exception
- }
- return bitmap;
- }
-
- @Override
- protected void onProgressUpdate(Integer... values) {
- // TODO Auto-generated method stub
- /*
- * 當publichProcess 呗調用以後,UI線程將調用這個有方法在界面上展示任務的情況,比如一個額進度條。這裡是更新進度條
- */
- int value=values[0];
- progressBar.setProgress(value);
- super.onProgressUpdate(values);
- }
-
-
- @Override
- protected void onPostExecute(Bitmap result) {
- // TODO Auto-generated method stub
- /*
- * 在doInbackground執行完成以後,onPostExecute將被調用,後台的結果將返回給UI線程,將獲得圖片顯示出來
- */
- imageView.setImageBitmap(result);
- button.setText("下載完成");
- super.onPostExecute(result);
- }
-
-
- }
加上權限<uses-permission android:name="android.permission.INTERNET"></uses-permission>