AsyncTask定义了三种泛型类型 Params,Progress和Result。
•Params 启动任务执行的输入参数,比如HTTP请求的URL。
•Progress 后台任务执行的百分比。
•Result 后台执行任务最终返回的结果,比如String。
Activity
01 |
/* |
02 |
* Android 异步任务AsyncTask |
03 |
* AsyncTask定义了三种泛型类,Params,Progress,Result |
04 |
*/ |
05 |
package rw.ansync; |
06 |
07 |
import android.app.Activity; |
08 |
import android.os.Bundle; |
09 |
import android.view.View; |
10 |
import android.view.View.OnClickListener; |
11 |
import android.widget.Button; |
12 |
import android.widget.EditText; |
13 |
import android.widget.ImageView; |
14 |
import android.widget.ProgressBar; |
15 |
16 |
public class AsynaTask_TestActivity extends Activity { |
17 |
/** Called when the activity is first created. */ |
18 |
private EditText editText; |
19 |
private Button button; |
20 |
private ProgressBar progressBar; |
21 |
private ImageView imageView; |
22 |
private static final String URL_STRING= "http://photocdn.sohu.com/20110927/Img320705637.jpg" ; |
23 |
@Override |
24 |
public void onCreate(Bundle savedInstanceState) { |
25 |
super .onCreate(savedInstanceState); |
26 |
setContentView(R.layout.main); |
27 |
|
28 |
editText=(EditText) findViewById(R.id.editText1); |
29 |
editText.setText(URL_STRING); |
30 |
button=(Button) findViewById(R.id.button1); |
31 |
progressBar=(ProgressBar) findViewById(R.id.progressBar1); |
32 |
imageView=(ImageView) findViewById(R.id.imageView1); |
33 |
button.setOnClickListener( new ButtonListener()); |
34 |
} |
35 |
class ButtonListener implements OnClickListener{ |
36 |
37 |
@Override |
38 |
public void onClick(View v) { |
39 |
// TODO Auto-generated method stub |
40 |
MyAsynaTask mTask= new MyAsynaTask(imageView, progressBar,button); |
41 |
mTask.execute(URL_STRING); |
42 |
} |
43 |
|
44 |
} |
45 |
|
46 |
|
47 |
} |
MyAsynaTask.java
001 |
package rw.ansync; |
002 |
003 |
import java.io.ByteArrayOutputStream; |
004 |
import java.io.File; |
005 |
import java.io.FileOutputStream; |
006 |
import java.io.InputStream; |
007 |
import java.io.OutputStream; |
008 |
import java.net.HttpURLConnection; |
009 |
import java.net.URL; |
010 |
011 |
012 |
013 |
import android.R.integer; |
014 |
import android.graphics.Bitmap; |
015 |
import android.graphics.BitmapFactory; |
016 |
import android.os.AsyncTask; |
017 |
import android.os.IInterface; |
018 |
import android.util.Log; |
019 |
import android.widget.Button; |
020 |
import android.widget.ImageView; |
021 |
import android.widget.ProgressBar; |
022 |
023 |
024 |
public class MyAsynaTask extends AsyncTask<string, bitmap= "" integer,= "" >{ |
025 |
|
026 |
private ImageView imageView; |
027 |
private ProgressBar progressBar; |
028 |
private Button button; |
029 |
|
030 |
public MyAsynaTask( ImageView imageView, |
031 |
ProgressBar progressBar,Button button) { |
032 |
super (); |
033 |
this .imageView = imageView; |
034 |
this .progressBar = progressBar; |
035 |
this .button=button; |
036 |
} |
037 |
038 |
039 |
@Override |
040 |
protected void onPreExecute() { |
041 |
// TODO Auto-generated method stub |
042 |
|
043 |
/* |
044 |
* 改方法在执行实际的后台操作时被UI线程调用,可以在该方法中做一些准备工作,比如 |
045 |
* Toast.makeText(context, "准备下载", Toast.LENGTH_LONG).show(); |
046 |
*/ |
047 |
super.onPreExecute(); |
048 |
} |
049 |
050 |
@Override |
051 |
protected Bitmap doInBackground(String... params) {//输入编变长的可变参数 和UI线程中的Asyna.execute()对应 |
052 |
// TODO Auto-generated method stub |
053 |
/* |
054 |
* 该方法在OnpreExecute执行以后马上执行,改方法执行在后台线程当中,负责耗时的计算,可以调用publishProcess方法来实时更新任务进度 |
055 |
*/ |
056 |
Bitmap bitmap=null; |
057 |
try { |
058 |
URL url=new URL(params[0]); |
059 |
Log.i("------------->", url+""); |
060 |
HttpURLConnection connection=(HttpURLConnection) url.openConnection(); |
061 |
|
062 |
connection.connect(); |
063 |
int MAX=connection.getContentLength(); |
064 |
progressBar.setMax(MAX); |
065 |
|
066 |
InputStream inputStream=connection.getInputStream(); |
067 |
|
068 |
ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); |
069 |
/* |
070 |
* 为了显示进度条,每接受1024字节要求更新一次UI,为了看效果 |
071 |
*/ |
072 |
byte []buf=new byte[1024]; |
073 |
int len = 0; |
074 |
int processBarNum=0; |
075 |
while( (len=inputStream.read(buf))!=-1){ |
076 |
outputStream.write(buf, 0, len); |
077 |
processBarNum+=len; |
078 |
|
079 |
publishProgress(processBarNum);//通知要更新processBar |
080 |
} |
081 |
bitmap=BitmapFactory.decodeByteArray(outputStream.toByteArray(),0, MAX); |
082 |
inputStream.close(); |
083 |
|
084 |
} catch (Exception e) { |
085 |
// TODO: handle exception |
086 |
} |
087 |
return bitmap; |
088 |
} |
089 |
090 |
@Override |
091 |
protected void onProgressUpdate(Integer... values) { |
092 |
// TODO Auto-generated method stub |
093 |
/* |
094 |
* 当publichProcess 呗调用以后,UI线程将调用这个有方法在界面上展示任务的情况,比如一个额进度条。这里是更新进度条 |
095 |
*/ |
096 |
int value=values[0]; |
097 |
progressBar.setProgress(value); |
098 |
super.onProgressUpdate(values); |
099 |
} |
100 |
101 |
|
102 |
@Override |
103 |
protected void onPostExecute(Bitmap result) { |
104 |
// TODO Auto-generated method stub |
105 |
/* |
106 |
* 在doInbackground执行完成以后,onPostExecute将被调用,后台的结果将返回给UI线程,将获得图片显示出来 |
107 |
*/ |
108 |
imageView.setImageBitmap(result); |
109 |
super .onPostExecute(result); |
110 |
} |
111 |
112 |
113 |
}</string,> |
加上权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>
转自:http://blog.csdn.net/rwyz1314/article/details/6841639