• Android HandlerThread的用法


    HandlerThread 继承自Thread,内部封装了Looper。

    首先Handler和HandlerThread的主要区别是:Handler与Activity在同一个线程中,HandlerThread与Activity不在同一个线程,而是别外新的线程中(Handler中不能做耗时的操作)。

    用法:

    import android.app.Activity;  
    import android.os.Bundle;  
    import android.os.Handler;  
    import android.os.Handler.Callback;  
    import android.os.HandlerThread;  
    import android.os.Message;  
      
      
    public class MainActivity extends Activity {  
        HandlerThread handlerThread = new HandlerThread("test");  
        Handler handler;  
          
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            System.out.println("cur_id="+Thread.currentThread().getId());  
              
            handlerThread.start();  
            System.out.println("handlerThread.id=" + handlerThread.getId());  
            //post(runnable),只是直接运行了run(),run()内ThreadId与UIThread是一样的  
    //      handler = new Handler();   
            //post(runnable),将runnable运行在handlerThread中,这是非UIThread的  
            handler = new Handler(handlerThread.getLooper(), new Callback() {  
                  
                @Override  
                public boolean handleMessage(Message msg) {  
                    System.out.println("receive message.whatA=" + msg.what);  
                    if (msg.what == 1) {  
                        return true;//不再向外层传递  
                    } else {  
                        return false; //外层的handleMessage() 继续执行  
                    }  
                      
                }  
            }) {  
                @Override  
                public void handleMessage(Message msg) {  
                    // TODO Auto-generated method stub  
                    super.handleMessage(msg);  
                    System.out.println("receive message.whatB=" + msg.what);  
                }  
            };  
            handler.post(new Runnable() {  
                  
                @Override  
                public void run() {  
                    System.out.println("handler_post_cur_id="+Thread.currentThread().getId());  
                    handler.sendEmptyMessage(1);  
                    handler.sendEmptyMessage(2);  
                }  
            });  
              
        }  
      
    }  

    运行结果:

    01-23 07:12:31.590: I/System.out(12386): cur_id=1  
    01-23 07:12:31.590: I/System.out(12386): handlerThread.id=1866  
    01-23 07:12:31.590: I/System.out(12386): handler_post_cur_id=1866  
    01-23 07:12:31.600: I/System.out(12386): receive message.whatA=1  
    01-23 07:12:31.600: I/System.out(12386): receive message.whatA=2  
    01-23 07:12:31.600: I/System.out(12386): receive message.whatB=2  
  • 相关阅读:
    bat 设置 IP, 清除IP
    Java Web 过滤器的典型应用
    Servlet 生命周期
    Java 的四类八种基本类型
    MVC框架实现登陆功能
    Android开发入门(2)创建Android项目
    Android开发入门(1)Android系统简介
    算法导论——排序复习
    eclipse添加maven
    centos编译openjdk
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5366496.html
Copyright © 2020-2023  润新知