Android后台进程与UI进程的交互

当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分发到对应的组件进行处理。所以主线程通常又被叫做UI线程。 

比如说从网上获取一个图片,在一个ImageView中将其显示出来,这种涉及到网络操作的程序一般都是需要开一个线程完成网络访问,但是在获得图片后,是不能直接在网络操作线程中调用ImageView的相关方法的,因为其他线程中是不能直接访问主UI线程成员 。

完整代码:

注意的是:Manifest.xml文件中,要声明网络权限


001 package com.xsjayz.thread;
002  
003 import org.apache.http.HttpResponse;
004 import org.apache.http.client.HttpClient;
005 import org.apache.http.client.methods.HttpGet;
006 import org.apache.http.impl.client.DefaultHttpClient;
007  
008 import android.app.Activity;
009 import android.graphics.Bitmap;
010 import android.graphics.BitmapFactory;
011 import android.os.Bundle;
012 import android.os.Handler;
013 import android.os.Message;
014 import android.view.View;
015 import android.view.View.OnClickListener;
016 import android.widget.Button;
017 import android.widget.ImageView;
018 import android.widget.Toast;
019  
020
026 public class ThreadHandlerActivity extends Activity {
027  
028     private static final int MSG_SUCCESS = 0;// 获取图片成功的标识
029     private static final int MSG_FAILURE = 1;// 获取图片失败的标识
030  
031     private ImageView mImageView;
032     private Button mButton;
033     private Thread mThread;
034  
035     @Override
036     public void onCreate(Bundle savedInstanceState) {
037         super.onCreate(savedInstanceState);
038         setContentView(R.layout.main);
039  
040         mImageView = (ImageView) findViewById(R.id.imageView);
041         mButton = (Button) findViewById(R.id.button);
042  
043         mButton.setOnClickListener(new OnClickListener() {
044             @Override
045             public void onClick(View v) {
046                 // 如果线程还没启动,则启动新的线程
047                 if (mThread == null) {
048                     mThread = new Thread(runnable);
049                     mThread.start();
050  
051                     // 否则提示:"线程已经启动"
052                 } else {
053                     Toast.makeText(
054                             getApplication(),
055                             getApplication().getString(R.string.thread_started),
056                             Toast.LENGTH_LONG).show();
057                 }
058             }
059         });
060     }
061  
062     private Handler mHandler = new Handler() {
063         // 重写handleMessage()方法,此方法在UI线程运行
064         @Override
065         public void handleMessage(Message msg) {
066             switch (msg.what) {
067             // 如果成功,则显示从网络获取到的图片
068             case MSG_SUCCESS:
069                 mImageView.setImageBitmap((Bitmap) msg.obj);
070                 Toast.makeText(getApplication(),
071                         getApplication().getString(R.string.get_pic_success),
072                         Toast.LENGTH_LONG).show();
073                 break;
074             // 否则提示失败
075             case MSG_FAILURE:
076                 Toast.makeText(getApplication(),
077                         getApplication().getString(R.string.get_pic_failure),
078                         Toast.LENGTH_LONG).show();
079                 break;
080             }
081         }
082     };
083  
084     Runnable runnable = new Runnable() {
085         // 重写run()方法,此方法在新的线程中运行
086         @Override
087         public void run() {
088             HttpClient httpClient = new DefaultHttpClient();
089             // 从网络上获取图片
090             HttpGet httpGet = new HttpGet(
091                     "http://www.oschina.net/img/logo.gif");
092             final Bitmap bitmap;
093             try {
094                 HttpResponse httpResponse = httpClient.execute(httpGet);
095                 // 解析为图片
096                 bitmap = BitmapFactory.decodeStream(httpResponse.getEntity()
097                         .getContent());
098             } catch (Exception e) {
099                 mHandler.obtainMessage(MSG_FAILURE).sendToTarget();// 获取图片失败
100                 return;
101             }
102  
103             // 获取图片成功,向UI线程发送MSG_SUCCESS标识和bitmap对象
104             mHandler.obtainMessage(MSG_SUCCESS, bitmap).sendToTarget();
105         }
106     };
107  
108 }


你可能感兴趣的:(JAVA)