android 广播问题

来源:互联网 发布:猎头公司 知乎 编辑:程序博客网 时间:2024/05/18 02:33
在android中,有静态和动态注册之分。但具体的操作来讲,动态注册还是使用比较方便的。如果想在接收到消息之后更改UI线程的内容,则可将广播receive的代码写在mainactivity里面,则后面的activity中一样可以调用这里。
        IntentFilter intentfilter = new IntentFilter();        intentfilter.addAction(MY_ACTION);        intentfilter.addAction(MY_ACTIONSEC);        receiver = new broadcastReceiver();        registerReceiver(receiver, intentfilter);
OnClickListener OnclickListener = new OnClickListener() {        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            Log.i(TAG, "ONCLICK");             Intent intent  = new Intent();               intent.setAction(MY_ACTION);               intent.putExtra("msg", "asdsdas");               sendBroadcast(intent);          }    };

重点是 sendBroadcast(intent),广播才会发送,如果只注册而没有这句代码,也是不行的。

代码下载地址:http://download.csdn.net/detail/happyhenji1213/8855013

0 0
原创粉丝点击