android 主线程和子线程之间的消息传递
Posted changhaiSmile
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 主线程和子线程之间的消息传递相关的知识,希望对你有一定的参考价值。
从主线程发送消息到子线程(准确地说应该是非UI线程)<span style="font-size:18px;">public class LooperThreadActivity extends Activity
/** Called when the activity is first created. */
private final int MSG_HELLO = 0;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new CustomThread().start();//新建并启动CustomThread实例
findViewById(R.id.send_btn).setOnClickListener(new OnClickListener()
@Override
public void onClick(View v) //点击界面时发送消息
String str = "hello";
Log.d("Test", "MainThread is ready to send msg:" + str);
mHandler.obtainMessage(MSG_HELLO, str).sendToTarget();//发送消息到CustomThread实例
);
class CustomThread extends Thread
@Override
public void run()
//建立消息循环的步骤
Looper.prepare();//1、初始化Looper
mHandler = new Handler()//2、绑定handler到CustomThread实例的Looper对象
public void handleMessage (Message msg) //3、定义处理消息的方法
switch(msg.what)
case MSG_HELLO:
Log.d("Test", "CustomThread receive msg:" + (String) msg.obj);
;
Looper.loop();//4、启动消息循环
</span>
从非UI线程传递消息到UI线程(界面主线程),因为主界面已经有MessageQueue,所以可以直接获取消息处理消息。而上面由主线程向非UI线程中处理消息的时候,非UI线程需要先添加消息队列,然后处理消息循环。
<span style="font-size:18px;">public class ThreadHandlerActivity extends Activity
/** Called when the activity is first created. */
private static final int MSG_SUCCESS = 0;//获取图片成功的标识
private static final int MSG_FAILURE = 1;//获取图片失败的标识
private ImageView mImageView;
private Button mButton;
private Thread mThread;
private Handler mHandler = new Handler()
public void handleMessage (Message msg) //此方法在ui线程运行
switch(msg.what)
case MSG_SUCCESS:
mImageView.setImageBitmap((Bitmap) msg.obj);//imageview显示从网络获取到的logo
Toast.makeText(getApplication(), getApplication().getString(R.string.get_pic_success), Toast.LENGTH_LONG).show();
break;
case MSG_FAILURE:
Toast.makeText(getApplication(), getApplication().getString(R.string.get_pic_failure), Toast.LENGTH_LONG).show();
break;
;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView= (ImageView) findViewById(R.id.imageView);//显示图片的ImageView
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if(mThread == null)
mThread = new Thread(runnable);
mThread.start();//线程启动
else
Toast.makeText(getApplication(), getApplication().getString(R.string.thread_started), Toast.LENGTH_LONG).show();
);
Runnable runnable = new Runnable()
@Override
public void run() //run()在新的线程中运行
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet("http://csdnimg.cn/www/images/csdnindex_logo.gif");//获取csdn的logo
final Bitmap bm;
try
HttpResponse hr = hc.execute(hg);
bm = BitmapFactory.decodeStream(hr.getEntity().getContent());
catch (Exception e)
mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败
return;
mHandler.obtainMessage(MSG_SUCCESS,bm).sendToTarget();//获取图片成功,向ui线程发送MSG_SUCCESS标识和bitmap对象
// mImageView.setImageBitmap(bm); //出错!不能在非ui线程操作ui元素
// mImageView.post(new Runnable() //另外一种更简洁的发送消息给ui线程的方法。
//
// @Override
// public void run() //run()方法会在ui线程执行
// mImageView.setImageBitmap(bm);
//
// );
;</span>
以上是关于android 主线程和子线程之间的消息传递的主要内容,如果未能解决你的问题,请参考以下文章