1. Define that when an event occurs, a broadcast will be sent, and all the recipients registered with the broadcast can receive the broadcast, that is to say, a broadcast can be received by multiple receivers. 2. Broadcast Reception. 1. Register in the configuration file (permanent registration, unless uninstalled)
Receiver Bundle bundle= }
<receiver android:name="com.example.receiver.Receiver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.ACTION_SHUTDOWN"/> </intent-filter> </receiver>
2. The code registers a temporary broadcast receiver to register the broadcast receiver (onCreate () when the activity is started, and cancels the broadcast receiver (onDestroy () when the activity exits ())
receiver = IntentFilter filter= IntentFilter("com.broadcast.ORDER" filter.setPriority(1000 }3. Send broadcast a. Normal broadcast: the receiver cannot be interrupted (absort), and data cannot be transmitted (result)
Intent intent= Intent("com.broadcast.NORMAL" sendBroadcast(intent,);B. Ordered broadcast: the receiver can be interrupted and data can be transmitted,
Intent intent= Intent("com.broadcast.ORDER" intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); sendOrderedBroadcast(intent,);4. Transmit data a and intent when sending a broadcast. The data cannot be modified (must be re-released) and added to intent.
Intent. putExtra ("data", "add data ");
Obtain corresponding data
intent.getStringExtra("data");
B. The ResultAPI transmits data, and ordered broadcast can use the ResultAPI to transmit data.
Bundle bundle = bundle. putString ("name", "Zhang San" bundle. putInt ("age", 21 sendOrderedBroadcast (intent, 1, "MainActivity", bundle );
Obtain data
code=getResultCode(); String data=getResultData(); Bundle bundle=getResultExtras();
The modified result data is in ordered broadcast. The receiver transmits the data to a chain structure, and the receiver can receive the modified data.
Bundle. putString ("name", "" bundle. putInt ("age", 25 setResult (6, "Paris", bundle );5. Interrupt broadcast (which can be operated in ordered broadcast) abortBroadcast () can interrupt the broadcast, and the broadcast will not be received later. If the third parameter in sendOrderedBroadcast () is assigned ResultReceiver, the broadcast will still be received, always the last receiver. 6. Broadcast permission. a. the sender requires the acceptor's permission (the receiver must have the permission)
The receiver must have the sending permission to receive the broadcast.
<Permission/> declare permission-declare this permission on the sender
<Uses-permission/> permission: use this permission on the acceptor to receive broadcasts.
B. the receiving end requires the sender's permission.
Specify the android: permission = "" permission in the <author ER/> label. The sender must have this permission to receive broadcasts from the receiver. If the receiver has no permission, the receiving end does not receive broadcasts. 7. The life cycle starts when the broadcast is received, ends when the onReceive () method is executed, and ends when the life cycle ends. If there are no other components in the process, it is easy to be killed.