android--broadcast broadcast "13"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.