In fact, in what is the first sentence of the broadcast, it has been explained what is the use of broadcasting. By the way, the general point is to transfer the data. The specific point is that:
1. Data transmission and sharing between different programs is achieved, because the broadcast is acceptable to the recipient as long as it is the same as the action that sent the broadcast.
The typical application is Android's own SMS, phone, and so on, as long as we achieve their action broadcast, then we can receive their data,
To make some processing. Like intercept system messages, intercept harassment calls, etc.
2. The role of a notification, such as in the service to notify the main program, update the main program UI, etc.
Two ways to register a broadcast:
Android offers two types of registered broadcast recipients,
are dynamically registered in the program and specified in the XML, respectively. The difference between them is that the scope of action is different,
The program is dynamically registered: The recipient is only valid during the program's operation,
And in the XML register: The recipient doesn't matter if your program has a startup, it works.
The broadcast sender is divided into ordinary broadcasting and orderly broadcasting;
Synchronous broadcast: When the sender sends out, it reaches more than one broadcast receiver at the same time.
A recipient cannot receive a broadcast after it has been processed and passed to the next recipient.
and unable to stop the broadcast from continuing to spread; Context.sendbroadcast (intent);
Ordered broadcast: The broadcast receiver needs to set priority ahead, priority first received broadcast, priority value is -1000~1000
Receive broadcast:
Private final String action_name = "Send broadcast";
Method of initialization
Public voidRegisterboradcastreceiver () {intentfilter myintentfilter=NewIntentfilter (); Myintentfilter.addaction (Action_name); //Register a broadcastregisterreceiver (mbroadcastreceiver, myintentfilter); } PrivateBroadcastreceiverMbroadcastreceiver=NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (action_name)) {Toast.maketext (Test. This, "processing the broadcast corresponding to the action name", 200); } } };
Send broadcast:
Public void Sendboradcastreceiver () { new Intent (action_name); Mintent.putextra ("name", "send broadcast, equivalent to transmit data here"); // Send broadcast sendbroadcast (mintent); }
Method of stopping:
unregisterReceiver(mBroadcastReceiver);
Android Broadcast recipient