继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

hander,message简单用法(基础用法)

一只名叫tom的猫
关注TA
已关注
手记 500
粉丝 62
获赞 330

千变万化,都离不开基础 ,基础越牢固,,走的越远,

message发送的几种方式,看源码最好了。

private class  MyThread implements Runnable{
        @Override
        public void run() {
            /**
             * 第一种方式
             */
//            Message message=Message.obtain();
//            message.what=1;
//            message.arg1=2;
//            message.arg2=3;
//            message.obj="wo de dongxi";
//            mHandler.sendMessage(message);
            /**
             * 第二种
             */
//            Message message=Message.obtain(mHandler);
//            message.what=1;
//            message.arg1=2;
//            message.arg2=3;
//            message.obj="wo de dongxi";
//            message.sendToTarget();
            /**
             * 第三种
             */
//            Message message=Message.obtain(mHandler,33,1,2,"haha");
//            message.sendToTarget();
            /**
             * 第四种
             */
            Message message=Message.obtain(mHandler,33,1,2,"haha");
            Bundle mBundle=new Bundle();
            mBundle.putStringArray("handler",new String[]{"java","android","html"});
            message.setData(mBundle);
            message.sendToTarget();

        }
    }
handler的发送消息,不用message,用handler自带的开启发送,
   new Thread(new Runnable() {
                @Override
                public void run() {
                    /**
                     * 几秒之后发送消息
                     */
//                    mHandler.sendEmptyMessage(3);
//                    mHandler.sendEmptyMessageAtTime(3,1000);
//                    mHandler.sendEmptyMessageDelayed(3,3000);
                }
            }).start();

        }else if (v==mB2){
            /**
             * 有一种发送消息
             */
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(3);
                }
            });
            /**
             * 四秒发送
             */
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {

                }
            },4);
子线城用法
private class MyThread implements Runnable{
    @Override
    public void run() {
        android.os.Looper.prepare();
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d("Looper",msg.obj.toString());
                mView.setText(msg.obj.toString());

            }
        };
        android.os.Looper.loop();

    }

原文链接:http://www.apkbus.com/blog-340477-72620.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP