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();

}

}

 

posted @ 2016-05-24 11:32  构建巨人肩膀  阅读(193)  评论(0编辑  收藏  举报