首页 技术 正文
技术 2022年11月7日
0 收藏 388 点赞 821 浏览 3523 个字

MainActivity如下:

package cc.c;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
/**
* Demo描述:
*
* 示例步骤如下:
* 1 子线程给子线程本身发送消息
* 2 收到1的消息后,子线程给主线程发送消息
* 3 收到2的消息后,主线程给子线程发送消息
*
* 为实现子线程给自己本身发送消息,关键还是在于构造Handler时传入的Looper.
* 在此就传入该子线程自己的Looper即调用Looper.myLooper(),代码如下:
* Looper.prepare();
* mHandlerTest1=new HandlerTest1(Looper.myLooper());
* Looper.loop();
*
* 所以当mHandlerTest1.sendMessage(message);发送消息时
* 当然是发送到了它自己的消息队列.
*
* 当子线程中收到自己发送的消息后,可继续发送消息到主线程.此时只要注意构造
* Handler时传入的Handler是主线程的Handler即可,即getMainLooper().
* 其余没啥可说的.
*
*
* 在主线程处理消息后再发消息到子线程
*
*
* 其实这些线程间发送消息,没有什么;关键还是在于构造Handler时传入谁的Looper.
*
*/
public class MainActivity extends Activity {
private TextView mTextView;
private HandlerTest1 mHandlerTest1;
private HandlerTest2 mHandlerTest2;
private int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}private void init() {
mTextView = (TextView) findViewById(R.id.textView);//1 子线程发送消息给本身
new Thread() {
public void run() {
Looper.prepare();
mHandlerTest1=new HandlerTest1(Looper.myLooper());
Message message = new Message();
message.obj = "子线程发送的消息Hi~Hi";
mHandlerTest1.sendMessage(message);
Looper.loop();
};
}.start();}private class HandlerTest1 extends Handler {private HandlerTest1(Looper looper) {
super(looper);
}@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("子线程收到:" + msg.obj);//2 收到消息后可再发消息到主线程
mHandlerTest2=new HandlerTest2(getMainLooper());
Message message = new Message();
message.obj = "O(∩_∩)O";
mHandlerTest2.sendMessage(message);
}
}private class HandlerTest2 extends Handler {private HandlerTest2(Looper looper) {
super(looper);
}@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mTextView.setText("在主线程中,收到子线程发来消息:" + msg.obj);//3 收到消息后再发消息到子线程
if (counter==0) {
Message message = new Message();
message.obj = "主线程发送的消息Xi~Xi";
mHandlerTest1.sendMessage(message);
counter++;
}}
}}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true"
android:layout_marginTop="70dip" /></RelativeLayout>

对于Android Handler与Message的多线程消息的处理,为什我以下代码会死掉? 看看,

 

1.线程没有终止条件,会一直给主线程发消息,主线程不停的调用handleMessage代码,很容易ANR(应用程序不响应)
2.handler.obtainMessage()得到message对象比new Message();更高效

Android Handler机制 怎使用?

 

Handler对象与其调用者在同一线程中,如果在Handler中设置了延时操作,则调用线程也会堵塞。每个Handler对象都会绑定一个Looper对象,每个Looper对象对应一个消息队列(MessageQueue)。如果在创建Handler时不指定与其绑定的Looper对象,系统默认会将当前线程的Looper绑定到该Handler上。
在主线程中,可以直接使用new Handler()创建Handler对象,其将自动与主线程的Looper对象绑定;在非主线程中直接这样创建Handler则会报错,因为Android系统默认情况下非主线程中没有开启Looper,而Handler对象必须绑定Looper对象。这种情况下,需先在该线程中手动开启Looper(Looper.prepare()–>Looper.loop()),然后将其绑定到Handler对象上;或者通过Looper.getMainLooper(),获得主线程的Looper,将其绑定到此Handler对象上。
Handler发送的消息都会加入到Looper的MessageQueue中。一说Handler包含两个队列:线程队列和消息队列;使用Handler.post()可以将线程对象加入到线程队列中;使用Handler.sendMessage()可以将消息对象加入到消息队列中。通过源码分析证实,Handler只有一个消息队列,即MessageQueue。通过post()传进去的线程对象将会被封装成消息对象后传入MessageQueue。
使用post()将线程对象放到消息队列中后,当Looper轮询到该线程执行时,实际上并不会单独开启一个新线程,而仍然在当前Looper绑定的线程中执行,Handler只是调用了该线程对象的run()而已。如,在子线程中定义了更新UI的指令,若直接开启将该线程执行,则会报错;而通过post()将其加入到主线程的Looper中并执行,就可以实现UI的更新。
使用sendMessage()将消息对象加入到消息队列后,当Looper轮询到该消息时,就会调用Handler的handleMessage()来对其进行处理。再以更新UI为例,使用这种方法的话,就先将主线程的Looper绑定在Handler对象上,重载handleMessage()来处理UI更新,然后向其发送消息就可以了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,947
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,473
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,286
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,102
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,734
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,769