您的位置:首页 > 其它

发送和接收无序广播

2016-10-21 09:06 169 查看
发送广播:
1.在需要发送的地方设置意图动作,发送广播
<span style="white-space:pre">		</span>Intent intent = new Intent();
intent.putExtra("data", "这是一条新的消息");
intent.setAction("com.skxy.unorder.MainActivity.FM");
sendBroadcast(intent);//发送广播,不需要在清单文件注册


接收无序广播:
1.定义类,继承BroadCastReceiver,重写onReceive方法
<span style="white-space:pre">		</span>String data = intent.getStringExtra("data");//通过键获取值

System.out.println("收到广播了:" + data);


2.在清单文件注册接收
<receiver android:name="com.skxy.reciver.MyReceiver">
<intent-filter >
<action android:name="com.skxy.unorder.MainActivity.FM"/>
</intent-filter>
</receiver>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  broadcastreceiver