美文网首页
handler跨类通信

handler跨类通信

作者: dev晴天 | 来源:发表于2018-08-11 19:37 被阅读0次
4  使 用 静态 handler 夸类传递消息

主类中:
   public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 获得数据
            Bundle bundle = msg.getData();

            during = bundle.getInt("during");
            currentPosition = bundle.getInt("currentPosition");
        }
    };

其他类中:

       int currentPosition = mediaPlayer.getCurrentPosition();//当前播放位置
                // 问题? 当前的两个值 怎样传到主activity ----  使用handler
              Message msg=  Message.obtain();
                // 使用msg发送多条数据
                Bundle bundle = new Bundle();// bundle 封装了map
                bundle.putInt("during",during);
                bundle.putInt("currentPosition",currentPosition);
                msg.setData(bundle);
              MainActivity.handler.sendMessage(msg);
            }

相关文章

网友评论

      本文标题:handler跨类通信

      本文链接:https://www.haomeiwen.com/subject/izadbftx.html