标签:
首先在AndroidMainifest中添加上网权限
1
|
<uses-permission android:name= "android.permission.INTERNET" ></uses-permission> |
布局文件activity_main.xml
Activity代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
public class MainActivity extends Activity{ private Button button; private ImageView imageView; private ProgressDialog progressDialog; private final String IMATH_PATH = http: //image16-c.poco.cn/best_pocoers/20141010/11092014101016572228935421.jpg; private AsyncTask<string,> task; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.btn); imageView = (ImageView)findViewById(R.id.img); progressDialog = new ProgressDialog( this ); progressDialog.setTitle(提示信息); progressDialog.setMessage(正在下载中,请稍后); progressDialog.setOnCancelListener( new OnCancelListener() { @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub task.cancel( true ); } }); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); button.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub task = new MyAsyncTask().execute(IMATH_PATH); } }); } class MyAsyncTask extends AsyncTask<string,>{ @Override protected void onPreExecute() { // TODO Auto-generated method stub super .onPreExecute(); progressDialog.setProgress( 0 ); progressDialog.show(); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super .onProgressUpdate(values); progressDialog.setProgress(values[ 0 ]); } @Override protected byte [] doInBackground(String... params) { // TODO Auto-generated method stub HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[ 0 ]); byte [] image = new byte []{}; try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); InputStream inputStream = null ; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); if (httpEntity != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ long file_length = httpEntity.getContentLength(); long total_length = 0 ; int length = 0 ; byte [] data = new byte [ 1024 ]; inputStream = httpEntity.getContent(); while (- 1 != (length = inputStream.read(data))){ total_length += length; byteArrayOutputStream.write(data, 0 , length); int progress = (( int )(total_length/( float )file_length)* 100 ); publishProgress(progress); } } image = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); } catch (Exception e){ e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return image; } @Override protected void onPostExecute( byte [] result) { // TODO Auto-generated method stub super .onPostExecute(result); Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0 , result.length); imageView.setImageBitmap(bitmap); progressDialog.dismiss(); } } } </string,></string,> |
讲解:
syncTask:异步任务,从字面上来说,就是在我们的UI主线程运行的时候,异步的完成一些操作。AsyncTask允许我们的执行一个异步的任务在后台。我们可以将耗时的操作放在异步任务当中来执行,并随时将任务执行的结果返回给我们的UI线程来更新我们的UI控件。通过AsyncTask我们可以轻松的解决多线程之间的通信问题。
怎么来理解AsyncTask呢?通俗一点来说,AsyncTask就相当于Android给我们提供了一个多线程编程的一个框架,其介于Thread和Handler之间,我们如果要定义一个AsyncTask,就需要定义一个类来继承AsyncTask这个抽象类,并实现其唯一的一个 doInBackgroud 抽象方法。要掌握AsyncTask,我们就必须要一个概念,总结起来就是: 3个泛型,4个步骤。
3个泛型指的是什么呢?我们来看看AsyncTask这个抽象类的定义,当我们定义一个类来继承AsyncTask这个类的时候,我们需要为其指定3个泛型参数:
1
|
AsyncTask <params, result= "" ></params,> |
我们在定义一个类继承AsyncTask类的时候,必须要指定好这三个泛型的类型,如果都不指定的话,则都将其写成Void,例如:
1
|
AsyncTask < void , void = "" ></ void ,> |
4个步骤:当我们执行一个异步任务的时候,其需要按照下面的4个步骤分别执行
结伴旅游,一个免费的交友网站:www.jieberu.com
推推族,免费得门票,游景区:www.tuituizu.com
标签:
原文地址:http://www.cnblogs.com/rabbit-bunny/p/4279716.html