zoukankan      html  css  js  c++  java
  • handler发消息的形式

    1、onCreate()中写好handler的接受机制,准备接受并处理消息

    2、thread中利用handler.post(Runnable r):

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView) findViewById(R.id.text);
            handler = new Handler();
    //        new myHandlerThread(handler).start();
            new myHandlerThread2(handler).start();
        }
    
    
        class myHandlerThread2 extends Thread {
            private Handler handler;
    
            public myHandlerThread2(Handler handler) {
                // TODO Auto-generated constructor stub
                this.handler = handler;
            }
    
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                handler.post(new Runnable() {
                    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        textView.setText("myHandlerThread2");
                    }
                });
            }
        }

    3、将一个线程打造成一个可以直接操作UI的线程(未尝试,可能不成功)

    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();
    
    }
    
    }
  • 相关阅读:
    io系列之常用流一
    C++ 函数参数的默认值
    C++ 函数匹配和作用域声明
    c++ vector 迭代器 demo
    C++ 函数重载和匹配
    C++函数重载和const
    C++函数重载
    iOS开源项目
    Linux系统/网络 笔记
    IO五种模式
  • 原文地址:https://www.cnblogs.com/yuan951/p/5522826.html
Copyright © 2011-2022 走看看