android多线程文章,Android 多线程处理之多线程用法大集合
類型:服務(wù)器區(qū)大小:21KB語言:中文 評分:6.6
標(biāo)簽:
立即下載
第 4 頁 全部源碼
全部源碼:
1 package com.bvin.exec;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.Executors;
8
9 import android.app.Activity;
10 import android.graphics.drawable.Drawable;
11 import android.os.AsyncTask;
12 import android.os.Bundle;
13 import android.os.Handler;
14 import android.os.HandlerThread;
15 import android.os.Message;
16 import android.util.Log;
17 import android.view.View;
18 import android.widget.Button;
19 import android.widget.ImageView;
20
21 public class MainActivity extends Activity {
22???? /** Called when the activity is first created. */
23
24???? private Handler handler ;
25???? private Button bt;
26???? private Handler mainHandler = new Handler(){
27
28???????? @Override
29???????? public void handleMessage(Message msg) {
30???????????? // TODO Auto-generated method stub
31???????????? super.handleMessage(msg);
32???????????? if(msg.what == 2012){
33???????????????? //只要在主線程就可以處理ui
34???????????????? ((ImageView)MainActivity.this.findViewById(msg.arg1)).setImageDrawable((Drawable)msg.obj);
35???????????? }
36???????? }
37
38
39???? };
40
41???? private ExecutorService service = Executors.newFixedThreadPool(5);
42
43???? @Override
44???? public void onCreate(Bundle savedInstanceState) {
45???????? super.onCreate(savedInstanceState);
46???????? setContentView(R.layout.main);
47???????? initViews();
48???????? HandlerThread ht = new HandlerThread("down image thread");
49???????? ht.start();
50???????? handler = new Handler(ht.getLooper()){//如果有了looper那么這個handler就不可以處理ui了
51
52???????????? @Override
53???????????? public void handleMessage(Message msg) {
54???????????????? // TODO Auto-generated method stub
55???????????????? super.handleMessage(msg);
56
57
58???????????? }
59
60???????? };
61
62???? }
63
64???? private void initViews(){
65
66???????? bt = (Button)findViewById(R.id.bt);
67???????? bt.setOnClickListener(new View.OnClickListener() {
68
69???????????? @Override
70???????????? public void onClick(View v) {
71???????????????? // TODO Auto-generated method stub
72???????????????? loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/23c1625aca99f02c50d8e510383a34e7.jpg",R.id.iv1);
73???????????????? loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/c4698d97ef6d10722c8e917733c7beb3.jpg",R.id.iv2);
74???????????????? loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f332ffe433be2a3112be15f78bff5a40.jpg",R.id.iv3);
75???????????????? loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/6ff8a9c647a1e80bc602eeda48865d4c.jpg",R.id.iv4);
76???????????????? loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f104d069f7443dca52a878d779392874.jpg",R.id.iv5);
77???????????? }
78???????? });
79???? }
80
81???? private void loadImagesByHandler(final String url,final int id){//通過擁有l(wèi)ooper的handler.post(runnable),新建線程
82
83
84???????? handler.post(new Runnable(){//如果handler沒有Looper那么它就不能構(gòu)建新線程了
85
86???????????? @Override
87???????????? public void run() {
88???????????????? // TODO Auto-generated method stub
89???????????????? Log.e("當(dāng)前線程:", ""+Thread.currentThread().getName());
90???????????????? Drawable drawable = null;
91???????????????? try {
92???????????????????? drawable = Drawable.createFromStream(new URL(url).openStream(), "image.gif");
93???????????????? } catch (MalformedURLException e) {
94???????????????????? // TODO Auto-generated catch block
95???????????????????? e.printStackTrace();
96???????????????? } catch (IOException e) {
97???????????????????? // TODO Auto-generated catch block
98???????????????????? e.printStackTrace();
99???????????????? }
100???????????????? //SystemClock.sleep(2000);
101???????????????? //((ImageView)MainActivity.this.findViewById(id)).setImageDrawable(drawable);
102???????????????? Message msg = mainHandler.obtainMessage();
103???????????????? msg.what = 2012;
104???????????????? msg.arg1 = id;
105???????????????? msg.obj = drawable;
106???????????????? msg.sendToTarget();
107???????????? }
108
109???????? });
110
111
112???? }
113
114???? private void loadImagesByThread(final String url,final int id){//通過Thread來new 出多個線程
115
116???????? new Thread(new Runnable(){
117
118???????????? @Override
119???????????? public void run() {
120???????????????? // TODO Auto-generated method stub
121???????????????? Log.e("當(dāng)前線程:", ""+Thread.currentThread().getName());
122???????????????? Drawable drawable = null;
123???????????????? try {
124???????????????????? drawable = Drawable.createFromStream(new URL(url).openStream(), "image.gif");
125???????????????? } catch (MalformedURLException e) {
126???????????????????? // TODO Auto-generated catch block
127???????????????????? e.printStackTrace();
128???????????????? } catch (IOException e) {
129???????????????????? // TODO Auto-generated catch block
130???????????????????? e.printStackTrace();
131???????????????? }
132???????????????? Message msg = mainHandler.obtainMessage();
133???????????????? msg.what = 2012;
134???????????????? msg.arg1 = id;
135???????????????? msg.obj = drawable;
136???????????????? msg.sendToTarget();
137
138???????????? }
139
140???????? }).start();
141???? }
142
143???? private void loadImageByAsyncTask(final String url,final int id){//構(gòu)建異步任務(wù),這樣就不用handler來處理消息了
144???????? DownloadTask task = new DownloadTask();
145???????? task.execute(""+id,url);//AsyncTask不可重復(fù)執(zhí)行
146???? }
147
148???? private void loadImagesByExecutors(final String url,final int id){
149???????? service.submit(new Runnable(){
150
151???????????? @Override
152???????????? public void run() {
153???????????????? // TODO Auto-generated method stub
154???????????????? Log.e("當(dāng)前線程:", ""+Thread.currentThread().getName());
155
156???????????????? try {
157???????????????????? final Drawable drawable? = Drawable.createFromStream(new URL(url).openStream(), "image.gif");
158???????????????????? mainHandler.post(new Runnable(){
159
160???????????????????????? @Override
161???????????????????????? public void run() {//這將在主線程運行
162???????????????????????????? // TODO Auto-generated method stub
163???????????????????????????? ((ImageView)MainActivity.this.findViewById(id)).setImageDrawable(drawable);
164???????????????????????? }
165???????????????????? });
166
167???????????????? } catch (MalformedURLException e) {
168???????????????????? // TODO Auto-generated catch block
169???????????????????? e.printStackTrace();
170???????????????? } catch (IOException e) {
171???????????????????? // TODO Auto-generated catch block
172???????????????????? e.printStackTrace();
173???????????????? }
174
175???????????? }
176
177???????? });
178
179???? }
180
181???? class DownloadTask extends AsyncTask{
182
183???????? int id;
184???????? @Override
185???????? protected Drawable doInBackground(String... params) {//params保存url和控件id兩個數(shù)據(jù)
186???????????? // TODO Auto-generated method stub
187???????????? Log.e("當(dāng)前線程:", ""+Thread.currentThread().getName());
188???????????? Drawable drawable = null;
189???????????? this.id = Integer.parseInt(params[0]);
190???????????? try {
191???????????????? drawable = Drawable.createFromStream(new URL(params[1]).openStream(), "image.gif");
192???????????? } catch (MalformedURLException e) {
193???????????????? // TODO Auto-generated catch block
194???????????????? e.printStackTrace();
195???????????? } catch (IOException e) {
196???????????????? // TODO Auto-generated catch block
197???????????????? e.printStackTrace();
198???????????? }
199
200???????????? return drawable;
201???????? }
202
203???????? @Override
204???????? protected void onPostExecute(Drawable result) {
205???????????? // TODO Auto-generated method stub
206???????????? super.onPostExecute(result);
207???????????? ((ImageView)MainActivity.this.findViewById(id)).setImageDrawable(result);
208???????? }
209
210???????? @Override
211???????? protected void onPreExecute() {
212???????????? // TODO Auto-generated method stub
213???????????? super.onPreExecute();
214???????? }
215
216???????? @Override
217???????? protected void onProgressUpdate(Integer... values) {
218???????????? // TODO Auto-generated method stub
219???????????? super.onProgressUpdate(values);
220???????? }
221
222
223???? }
224 }
總結(jié)
以上是生活随笔為你收集整理的android多线程文章,Android 多线程处理之多线程用法大集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单片机产生可调方波(c语言),单片机产生
- 下一篇: android webview 获取网页