Introduction to broadcasting mechanism
Android broadcasts can be divided into two types, standard broadcast and orderly broadcast;
Standard broadcast: is a completely asynchronous broadcast, after the broadcast is sent, all the broadcast receivers are almost the same when the broadcast received.
Ordered broadcast: A fully synchronous broadcast, after the broadcast, only one message at a time can receive the broadcast. When the logic in this broadcast receiver is completed, the broadcast will continue to pass, so the broadcast receiver at this time is sequential, the priority of the broadcast receiver can first receive the broadcast message, and the front of the broadcast receiver can also truncate the broadcast being transmitted, In this case, the broadcast receiver will not be able to receive the broadcast message.
Dynamic Registration monitoring network changes
Let's make a good understanding of the radio by doing an example of monitoring network changes.
There are two ways to register a broadcast, to register in code and to register in Androidmanifest.xml, where the former is also known as dynamic registration, which is also known as static registration.
So how do you create a broadcast receiver? It is also very simple, let it inherit from Broadcasereceiver, and rewrite the OnReceive () method of the parent class, so that when the message arrives, the OnReceive () method is executed. The specific logic that is received after the broadcast can be processed here.
1, in Getsystemservice This method, the code does not intercept complete, the complete code is the context. Connectivity_service
2, the system in order to protect the privacy of users, it is necessary for us to obtain network rights, this is the Androidmanifest.xml file registration
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
Note that it and the application tag are siblings, not included in the application tag.
Static registration
We are talking about dynamic registration, we can see the dynamic registration broadcast receiver can freely control its registration and destruction, is very flexible, but we think of a scenario, our dynamic registration is in the OnCreate () method inside, Is it possible to receive a broadcast if the program has not yet started? The answer is yes, of course, this requires us to use static registration, but also the static registration we need to write next.
It is created in the following ways:
1, the road to create the following file-new-other you can see broadcast Receiver
2. Exported indicates whether this broadcast is allowed to receive broadcasts other than this program
3. Enable to indicate whether this broadcast is enabled
The code we need to write after the creation is all in Androidmanifest.xml, and the problems involved are all in the following code:
In addition: we create the staticreceiver inside the code we do not see, that is, a onreceive method, there is a point to our attention, whether it is static or dynamic need our attention to the point is:
Note that the OnReceive method does not write too much logic code or other time-consuming operation, because in the broadcast receiver is not allowed to start a new thread, if the method is not responding for a long time, the program will error.
Send broadcast
Send a broadcast here we'll look directly at how the code is sending standard broadcasts and ordered broadcasts.
and a local broadcast. Our code is written together, what is local broadcasting for? I don't know if I remember what we said earlier. Static registered broadcasts can receive broadcasts from other applications, which reduces the security of the program and easily raises security issues! In order to solve this problem, Android introduced the concept of local broadcasting, local broadcast is the broadcast can only be issued in the interior of the program bed, and broadcast receivers can only receive from the application broadcast, improve the security of the program!
We have only registered and sent here, the registration of this part is not written, or in our OnDestroy method to write off.
Here's another thing we need to note is that the priority of the receiver after the ordered broadcast is issued, the code is as follows:
Here's what we've summed up about Android broadcast notifications!
Android Learning--broadcasting mechanism