Java并发编程(4)-- Handler,HandlerThread,IntentService

一、Handler

1、定义

Android系统中的视图组件并不是线程安全的,如果要更新视图,必须在主线程中更新,不可以在子线程中执行更新的操作。

那么,我们如何通知主线程呢?我们需要使用到Handler对象。

Handler主要有两个用途:首先是可以定时处理或者分发消息,其次是可以添加一个执行的行为在其它线程中执行。

2、用法

private Handler handler = new Handler() {  
    @Override  
    public void handleMessage(Message msg) {  
         if (msg.what == COMPLETED) {  
             stateText.setText("completed");  
         }  
    }  
};  
//工作线程  
    private class WorkThread extends Thread {  
        @Override  
        public void run() {  
            //......处理比较耗时的操作  
            //处理完成后给handler发送消息  
            Message msg = new Message();  
            msg.what = COMPLETED;  
            handler.sendMessage(msg);  
        }  
    } 

3、消息android.os.Message

android.os.Message是定义一个Messge包含必要的描述和属性数据,并且此对象可以被发送给android.os.Handler处理。

属性字段:arg1、arg2、what、obj、replyTo等;其中arg1和arg2是用来存放整型数据的;what是用来保存消息标示的;

obj是Object类型的任意对象;replyTo是消息管理器,会关联到一个handler,handler就是处理其中的消息。通常对Message

对象不是直接new出来的,只要调用handler中的obtainMessage方法来直接获得Message对象。

4、 Looper类

Looper类主要用于一个线程循环获取消息队列中的消息。

Looper的作用主要是负责管理消息队列,负责消息的出列和入列操作。

5、Handler内部实现机制

Android通过Looper、Handler来实现消息循环机制。Android的消息循环是针对线程的,每个线程都可以有自己的消息队列和消息循环。

Android系统中的Looper负责管理线程的消息队列和消息循环。通过Looper.myLooper()得到当前线程的Looper对象,

通过Looper.getMainLooper()得到当前进程的主线程的Looper对象。

前面提到,Android的消息队列和消息循环都是针对具体线程的,一个线程可以存在一个消息队列和消息循环,

特定线程的消息只能分发给本线程,不能跨线程和跨进程通讯。但是创建的工作线程默认是没有消息队列和消息循环的,

如果想让工作线程具有消息队列和消息循环,就需要在线程中先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。

 class MyThread extends Thread {
    public void run() {     
          // 其它线程中新建一个handler
          Looper.prepare();// 创建该线程的Looper对象,用于接收消息,在非主线程中是没有looper的,所以在创建handler前一定要使用prepare()创建一个Looper
          myThreadHandler = new Handler() {
               public void handleMessage(android.os.Message msg) {                  
               }
          };
          Looper.myLooper().loop();//建立一个消息循环,该线程不会退出
    }
 }

我们的Activity是一个UI线程,运行在主线程中,Android系统会在Activity启动时为其创建一个消息队列和消息循环。

Handler的作用是把消息加入特定的Looper所管理的消息队列中,并分发和处理该消息队列中的消息。

构造Handler的时候可以指定一个Looper对象,如果不指定则利用当前线程的Looper对象创建。

只要Handler对象以主线程的Looper创建,那么当调用Handler的sendMessage方法,系统就会把消息放入主线程的消息队列,并且将会在调用handleMessage方法时处理主线程消息队列中的消息。

Handler对象管理的Looper对象是线程安全的,不管是添加消息到消息队列还是从消息队列中读取消息都是同步保护的。

6、Handler.post(Runnable r)

把r加到消息队列,但并未开辟新线程。等到消息被取出时才执行。

7、为什么在 activity中使用handler为甚要声明为静态的?

同一个线程下的handler共享一个looper对象,消息中保留了对handler的引用,只要有消息在队列中,
那么handler便无法被回收,如果handler不是static,那么使用Handler的Service和Activity就也无法被回收,即便它们的ondestroy方法被调用。这就可能导致内存泄露。当然这通常不会发生,除非你发送了一个延时很长的消息。

但把hanlder添加为static后,会发现在handler中调用外部类的方法和成员变量需要它们都定义为final,这显然是不大可能的。

这里建议在你的Service或Activity中增加一个内部static Handler类,这个内部类持有Service或Activity的弱引用,这样就可以解决final的问题。

static class MyHandler extends Handler {  
                WeakReference<PopupActivity> mActivity;  
                MyHandler(PopupActivity activity) {  
                        mActivity = new WeakReference<PopupActivity>(activity);  
                }  
                @Override  
                public void handleMessage(Message msg) {  
                        PopupActivity theActivity = mActivity.get();  
                        switch (msg.what) {  
                        case 0:  
                                theActivity.popPlay.setChecked(true);  
                                break;  
                        }  
                }  
        };  
        MyHandler ttsHandler = new MyHandler(this);  
        private Cursor mCursor;  

        private void test() {  
                ttsHandler.sendEmptyMessage(0);  
        }  
} 

二、HandlerThread

HandlerThread是Android API提供的一个便捷的类,使用它我们可以快速的创建一个带有Looper的线程,有了Looper这个线程,我们又可以生成Handler。

public class HandlerThread extends Thread {
    int mPriority;
    int mTid = -1;
    Looper mLooper;

    public HandlerThread(String name) {
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    }
    //可选择优先级
    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }

    protected void onLooperPrepared() {
    }

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

    public Looper getLooper() {
        if (!isAlive()) {
            return null;
        }
        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    }
     /**
     * Causes the handler thread's looper to terminate without processing any
     * more messages in the message queue.
     * </p><p>
     * Any attempt to post messages to the queue after the looper is asked to quit will fail.
     * For example, the {@link Handler#sendMessage(Message)} method will return false.
     * Using this method may be unsafe because some messages may not be delivered
     * before the looper terminates.  Consider using {@link #quitSafely} instead to ensure
     */
    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }

    public boolean quitSafely() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quitSafely();
            return true;
        }
        return false;
    }

    public int getThreadId() {
        return mTid;
    }
}

三、IntentService

IntentService是一个基于Service的一个类,里面使用HandlerThread来处理异步的请求。你可以通过startService(Intent)来提交请求,该Service会在需要的时候创建,当完成所有的任务以后自己关闭,且请求是在工作线程处理的。
IntentService最起码有两个好处,一方面不需要自己去new Thread了;另一方面不需要考虑在什么时候关闭该Service了。

public abstract class IntentService extends Service {
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;
    private String mName;
    private boolean mRedelivery;

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }

    public IntentService(String name) {
        super();
        mName = name;
    }

    /**
     * Sets intent redelivery preferences.  Usually called from the constructor
     * with your preferred semantics.
     *
     * <p>If enabled is true,
     * {@link #onStartCommand(Intent, int, int)} will return
     * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
     * {@link #onHandleIntent(Intent)} returns, the process will be restarted
     * and the intent redelivered.  If multiple Intents have been sent, only
     * the most recent one is guaranteed to be redelivered.
     *
     * <p>If enabled is false (the default),
     * {@link #onStartCommand(Intent, int, int)} will return
     * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
     * dies along with it.
     */
    public void setIntentRedelivery(boolean enabled) {
        mRedelivery = enabled;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        //在onDestroy中释放我们的Looper
        mServiceLooper.quit();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @WorkerThread
    protected abstract void onHandleIntent(Intent intent);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值