第三章--Handler的二三事

本来写了开场白,算了,直接进入主题。

1、什么是Handler,作用是啥,为什么人人用了都说好。

Handler,我去查了一下字典,有处理者管理者的意思,它主要是负责线程之间的通信,比如UI线程(主线程)和其他线程之间的。那为什么一定要它负责通信呢,直接操作ui不行吗?当然不行,如果多个线程同时改变界面的属性值,那会变的很混乱,也叫做会导致线程安全问题。为此Android有一条必须遵守的规则:只允许UI线程修改Activity里的UI组件,是不是很霸气。但是这样其他线程就无法动态改变UI属性了,制定规则当然不是为了堵住一条路,Handler的作用这时候就体现出来了。
1、在新启动的线程中发送消息。
2、在主线程中处理消息。
比如你想改变界面上一张图片,线程中发个消息,Handler收到后立马乖乖的处理,然后给你修改。这么听话的东西怎么会没有人喜欢呢。

2、Handler简单使用

先贴代码

public class MainActivity extends Activity{
    //定义一个图片id数组
    int[] imageIds = new int[]{...};
    定义当前的显示的图片id
    int currentId = 0;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView show = (ImageView)findViewById(R.id.show);
        final Handler myHandler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what == 0x1233){
                    //修改照片
                    show.setImageResource(imageIds[currentId++] % imageIds.length);
                }
            }
        }
        //计时器
        new Timer().schedule(new TimerTask(){
            @Override
            public void run(){
                myHandler.sendMessage(0x1233);
            }
        }, 0, 1200);
    }

}

这样就完成了一个自动播放图片的效果,定义了一个定时器,让它定时的sendMessage,实质上就是开了一个新的线程,由于android不允许在新线程中访问Activity里的界面组件,所以只能发个消息咯,Handler也就定时的改变图片,并且是循环的,在这里不得不说算法好厉害。另外布局文件就只有一个ImageView。

3、Looper,MessageQueue

其实Handler不是单打独斗,它还有几个好朋友,Looper和MessageQueue。

先说Looper

  • 每个线程只能拥有一个Looper,不能有第二个,它的loop()方法就是读取MessageQueue中的消息,读到之后就会把消息交给发送消息的Handler处理。看到这有人可能会说那你上面的Looper呢?别急,这是因为在UI线程也就是主线程中,系统已经初始了一个Looper,所以我们就不用了啊~~。但是如果是自己启动的线程,我在Looper源码中发现注释里有个例子。
class LooperThread extends Thread {
       public Handler mHandler;

        public void run() {
            Looper.prepare();

            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    // process incoming messages here
                }
            };

            Looper.loop();
        }

先是Looper.prepare(),最后是Looper.loop()。我们来看看Looper.prepare()做了一些什么。

     /** Initialize the current thread as a looper.
      * This gives you a chance to create handlers that then reference 这能让你在创建Handler的时候能引用Looper
      * this looper, before actually starting the loop. Be sure to call 在Looper使用前,必须保证调用了该方法
      * {@link #loop()} after calling this method, and end it by calling 调用之后,在loop()就停止了
      * {@link #quit()}.
      */
    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        //这里应该就是new了Looper对象
        sThreadLocal.set(new Looper(quitAllowed));
    }

我用我蹩脚的英语翻译了一下,这个方法保证了线程中只有一个Looper对象。这似乎是使用了重载的方式调用,这样设计难道保证了封装的特性?
然后loop()的源码


    /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        final Looper me = myLooper();
        if (me == null) {
        //没有调用prepare().会抛出异常
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }

        final MessageQueue queue = me.mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        //发消息的标识符,final
        final long ident = Binder.clearCallingIdentity();
        //死循环来了
        for (;;) {
            Message msg = queue.next(); // might block 获取消息队列的下一个
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger 
            Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            msg.target.dispatchMessage(msg);

            if (logging != null) {
                logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
            }

            // Make sure that during the course of dispatching the
            // identity of the thread wasn't corrupted. 检查标识符是否变了。
            final long newIdent = Binder.clearCallingIdentity();
            if (ident != newIdent) {
                Log.wtf(TAG, "Thread identity changed from 0x"
                        + Long.toHexString(ident) + " to 0x"
                        + Long.toHexString(newIdent) + " while dispatching to "
                        + msg.target.getClass().getName() + " "
                        + msg.callback + " what=" + msg.what);
            }

            msg.recycleUnchecked();
        }
    }

就是从MessageQueue中不断取出消息

然后MessageQueue

消息队列,它有一点神秘,自始至终都没有见过它,在loop()方法的源码中有这样一个语句final MessageQueue queue = me.mQueue;莫非Looper的构造器什么的里就有它?再次去瞧瞧:

   private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

原来啊系统在创建Looper对象的时候,就会在它的构造器中创建该对象。而且MessageQueue是采用先进先出的方式管理消息。

3、总结一下

使用线程中的Handler:
1、Loop.prepare(),它构造器也会创建配套的MessageQueue对象
2、创建Handler实例,重写handleMessage()方法
3、Looper.loop()。启动Looper.
4、没有第四

4、Handler的内存泄漏问题

视频中说到,如果Activity结束了,但是和其有关联的Handler还没有处理完它的消息,如果Handler不是静态的,就不会被Activity或者Service回收,Handler使用的内存就不会被释放,这就造成了内存泄漏问题。这里的一个解决方法,就是弱引用。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了具有弱引用的对象,都会回收它的内存。

public static class Testhandler extends Handler{

        public final WeakReference<HandlerButtonActivity> mHandlerButoonActivityWeakReference;

        public Testhandler(HandlerButtonActivity activity){
            mHandlerButoonActivityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            HandlerButtonActivity activity = mHandlerButoonActivityWeakReference.get();
            ...}
   }

大概会写点异步,老师提了一下,所以没弄懂啊啊啊啊

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用中提到了http.Handler的ServeHTTP方法。在这个方法中,首先检查handler是否为空,如果为空,则使用默认的处理程序DefaultServeMux来处理请求。这意味着http.Handler是一个接口类型,它定义了ServeHTTP方法,并且可以根据需要被替换为不同的处理程序。在引用的示例代码中,自定义的myHandler类型实现了http.Handler接口,并定义了ServeHTTP方法来处理不同的URL路径。在main函数中,将自定义的myHandler作为参数传递给http.ListenAndServe函数来处理HTTP请求。这样,当有请求到达时,会根据URL路径来选择相应的处理程序。这种方式使得web服务器能够将请求分派给任意的http.Handler,而不需要考虑其具体类型。这也体现了在Go语言中,满足同一个接口的不同类型是可以替换的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [golang学习之go web 开发http.HandleFunc、http.ListenAndServe与http.Handler](https://blog.csdn.net/weixin_56349119/article/details/126335261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Go语言圣经 - 第7章 接口 - 7.7 http.Handler 接口](https://blog.csdn.net/weixin_51487151/article/details/122318673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值