android Handler Message传递参数

创建Handler

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
            case 1 :   String str1 = msg.getData().getString("text1");//接受msg传递过来的参数
                String str2 = msg.getData().getString("text2");//接受msg传递过来的参数
                Log.i("lgq","..ab ==7....11......"+str1);
                break;
        }
    }
};

传递参数

Message msg = new Message();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("text1","大明的消息传递参数的例子!");  //往Bundle中存放数据
bundle.putString("text2","Time:2016-11-03");  //往Bundle中put数据
msg.setData(bundle);//mes利用Bundle传递数据
handler.sendMessage(msg);//用activity中的handler发送消息

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/79551562
今日推荐