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