Android之Handler消息机制


Android中Handle类的主要作用:

1.在新启动的线程中发送给消息

2.在主线程获取、处理消息

 

为什么要用Handle这样的一个机制:

因为在Android系统中UI操作并不是线程安全的,如果多个线程并发的去操作同一个组件,可能导致线程安全问题。为了解决这一个问题,android制定了一条规则:只允许UI线程来修改UI组件的属性等,也就是说必须单线程模型,这样导致如果在UI界面进行一个耗时叫长的数据更新等就会形成程序假死现象 也就是ANR异常,如果20秒中没有完成程序就会强制关闭。所以比如另一个线程要修改UI组件的时候,就需要借助Handler消息机制了。

 

Handle发送和处理消息的几个方法:

1. void handleMessage(Message msg):处理消息的方法,该方法通常被重写。

2.final boolean hasMessage(int what):检查消息队列中是否包含有what属性为指定值的消息

3.final boolean hasMessage(int what ,Object object) :检查消息队列中是否包含有what好object属性指定值的消息

4.sendEmptyMessage(int what):发送空消息

5.final Boolean send EmptyMessageDelayed(int what ,long delayMillis):指定多少毫秒发送空消息

6.final boolean sendMessage(Message msg):立即发送消息

7.final boolean sendMessageDelayed(Message msg,long delayMillis):多少秒之后发送消息

 

与Handle工作的几个组件Looper、MessageQueue各自的作用:

1.Handler:它把消息发送给Looper管理的MessageQueue,并负责处理Looper分给它的消息

2.MessageQueue:采用先进的方式来管理Message

3.Looper:每个线程只有一个Looper,比如UI线程中,系统会默认的初始化一个Looper对象,它负责管理MessageQueue,不断的从MessageQueue中取消息,并将

相对应的消息分给Handler处理

 

在线程中使用Handler的步骤:

1.调用Looper的prepare()方法为当前线程创建Looper对象,创建Looper对象时,它的构造器会自动的创建相对应的MessageQueue

2.创建Handler子类的实例,重写HandleMessage()方法,该方法处理除UI线程以外线程的消息

3.调用Looper的loop()方法来启动Looper

 

实例

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >     <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />     <Button
        android:id="@+id/Ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ed1"
        android:text="@string/Ok" />
    <Button
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Ok"
        android:text="下一张" />     <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/next"
        android:src="@drawable/a3" /> </RelativeLayout>

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容

    暂无相关文章