Android------broadcastreceiver profile

Source: Internet
Author: User



Receive broadcast messages





One of the four components, essentially a global listener, is used to listen to the system's global broadcast messages, and the receiving program sends out the Broadcastresiver Intent

Therefore, it is very convenient to implement communication between different components in the system.




There are two main types of broadcasts in Android: Standard broadcast and ordered broadcast.


Standard broadcast (normal broadcasts) is a fully asynchronous broadcast that, after broadcast, all broadcast receivers receive this information almost at the same time
The efficiency of this broadcast is relatively high, but it also means that it cannot be truncated.


Ordered broadcast (Ordered broadcasts): is a synchronous broadcast that, after broadcast, is ordered by the broadcast receiver to receive broadcasts in order of priority.
And each broadcast receiver can operate on the broadcast, you can add data, you can truncate the broadcast, after truncation, the receiver will not be able to receive the broadcast.




Start Broadcastreceiver:

Create a intent that needs to start broadcastreceiver
Call Contex's Sendbroadcast () or Sendorderedbroadcast () method to start the specified broadcastreceiver



Send broadcast:

You only need to call Contex's Sendbroadcast (Intent Intent) method.



public class Mainactivity extends Activity {private Button send; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); send = (Button) Findviewbyid (R.id.send); Send.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubintent Intent = new Intent (); Intent.setaction (" Com.example.broadcastreceiverdemo.MYRECEIVER "); Intent.putextra (" msg "," simple Message "); Sendbroadcast (intent);}});}}


In this simple piece of code, only a button is included to send a broadcast.

Then there is the receiver of the broadcast, and the receiving broadcast simply inherits the Broadcastreceiver from the custom class and implements the OnReceive method.



public class Myreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {//TODO Auto-generated method Stubtoast.maketext (Context, "The received intent action is:" + intent.getaction () + "message content is:" + Intent.getstringextra ("msg"), 0). Show ();}}






Android------broadcastreceiver profile

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.