Broadcasting (broadcast) is a mechanism for propagating data (Intent) between components, which can even be located in different processes, playing the role of interprocess communication, mainly for the purpose of notification. The following is a demo of this example.
1. Define the message ID of the notification broadcast first
Private String Msg_broadcast = "Msg_broadcast";
2. Broadcast monitoring and processing of broadcast messages
private void Listen () {intentfilter filter = new Intentfilter (); filter.addaction (Msg_broadcast); MainActivity.this.registerReceiver (receiver, filter);} Private Broadcastreceiver receiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent inte NT) {String action = intent.getaction (), if (Msg_broadcast.equals (action)) {LOG.I (TAG, "recv MSG");}};
3. Broadcast monitoring stop
private void Stoplisten () {MainActivity.this.unregisterReceiver (receiver);}
4. Call in Onresume and OnPause events, respectively
@Overridepublic void Onresume () {super.onresume (); listen ();} @Overridepublic void OnPause () {super.onpause (); Stoplisten ();}
5. Test outgoing broadcasts
BTN = (Button) This.findviewbyid (R.id.button1); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {log.i (TAG, "btn Click Start") Intent Intent = new Intent (msg_broadcast); Intent.putextra ("Message", "tes T ");//Parameter Sendbroadcast (intent);}});
android--broadcast broadcast "13"