What is a broadcast
In Android, broadcast is a widely used mechanism for transmitting information between applications. Let's use a radio station for an analogy. We usually use radio radios to do this: a lot of different radio stations send their content on a specific frequency, and our users just need to tune in to the radio to listen to their content. The broadcasting mechanism in Android is similar to this.
The content of the radio is voice, and the content of the broadcast we are going to send in Android is a intent. This intent can carry the data we want to transmit.
The radio sends content through high-powered transmitters, and in Android it is sent by Sendbroadcast (a very graphic name).
The user receives the content of the radio by adjusting to the specific radio frequency. And in the Android to accept the content of the broadcast is to register a broadcastreceiver to receive. Only the action to send the broadcast and the action to receive the broadcast will be accepted by the recipient.
What's the use of broadcasting?
In fact, the first sentence of what is broadcast has already explained what the use of radio is. By the way, the general point is to transmit the data. The specific point is: 1. Data transmission and sharing between different programs is achieved, as as long as it is the same as the recipient of the action that sends the broadcast, this broadcast can be accepted. The typical application is Android's own text messages, phone calls and so on, so long as we can broadcast their action, we'll be able to receive their data to make some processing possible. such as intercepting system messages, intercepting harassing phones, etc. 2. Played a role in the notification, such as in the service to notify the main program, update the main program UI. Because the service has no interface, it is not possible to get the controls in the main program directly, so that we can only implement a broadcast recipient in the main program to accept the data and notifications sent by the service.
Implement broadcast
Now we're going to implement a simple broadcast program. Android provides two forms of registered broadcast recipients, which are dynamically registered in a program and specified in XML. The difference between them is that the scope of the function is different, the recipient of the program dynamic registration is only valid in the process of running the program, while the recipient of the XML registration regardless of whether or not your program has a start will work. First, describe how to dynamically register in your program.
Dynamic Registration method
We have set up three buttons in the program, namely "Register broadcast", "Cancel Registration" and "Send Broadcast". Then each button sets the Click event to complete the broadcast demo.
The simplest project establishment process and the establishment of the button event I will not be wordy here, will not be able to download the following demo source view. Look directly at the implementation of the three buttons.
The first is to register the broadcast button event code:
Copy Code code as follows:
Private Receivebroadcast Receivebroadcast; Broadcast instance
public class Registelinster implements Onclicklistener
{
@Override
public void OnClick (view view)
{
Registering for broadcast reception
Receivebroadcast = new Receivebroadcast ();
Intentfilter filter = new Intentfilter ();
Filter.addaction (flag); Only recipients who hold the same action can receive this broadcast
Registerreceiver (receivebroadcast, filter);
}
}
public class Receivebroadcast extends Broadcastreceiver
{
@Override
public void OnReceive (context context, Intent Intent)
{
Get the data from the broadcast and show it
String message = Intent.getstringextra ("Data");
Txtshow.settext (message);
}
}
First we implement a Receivebroadcast class that inherits the Broadcastreceiver and implements the OnReceive method, so that this method is executed when the broadcast is received. Note that we used the Filter.addaction method to add a filter when registering for the broadcast. Without this, it would be a bad idea to listen to radio stations without telling us how often the radios were received.
To see how to unregister, the program no longer receives this type of broadcast.
Copy Code code as follows:
public class Unregistelinster implements Onclicklistener
{
@Override
public void OnClick (View arg0)
{
Unregisterreceiver (Receivebroadcast);
}
}
What do you think? is not super simple Ah, it is the broadcast class above us to pass the example of the line. Register now, cancel registration is good, the rest how to send. Look at the code:
Copy code code as follows:
public class Sendbroadcastlistener implements Onclicklistener
{
@Override
public void OnClick (View arg0)
{
Intent Intent = new Intent (); Itent is what we're going to send.
Intent.putextra ("Data", "This are data from broadcast" +calendar.getinstance (). Get (Calendar.second));
Intent.setaction (flag); Set up your broadcast action, only recipients with this action can receive the broadcast
Sendbroadcast (Intent); Send broadcast
}
}
Every sentence is commented, don't let me say it again. I can see it. Now, run the program and see how it works. Register first, and then each time you send a broadcast the text will change once, indicating that you have received the broadcast. You may find that you can no longer receive the broadcast by pressing the Send button after you cancel the registration.
Configuration file Mode
The difference between configuration and dynamic registration has already been said that this approach to your program requires long-term monitoring of a broadcast situation, such as monitoring user text messages. Registration method is relatively simple, equivalent to the above code as long as the part of the line to receive. Note, however, that registering a broadcast this way through a configuration file requires inheriting Broadreceiver in a separate class, which is useless. So we created a new Broadcastreceivebyxml class and inherited the Broadreceive. The code is as follows:
Copy Code code as follows:
public class Broadcastreceivebyxml extends Broadcastreceiver
{
@Override
public void OnReceive (context arg0, Intent arg1)
{
LOG.D ("QLF", "Broadcast receive by XML"); Because it is not easy to use the control under the main UI, we print it to log inside to see the effect
}
}
Then we add the code after the <activity></activity> node in androidmanifest:
Copy Code code as follows:
<receiver android:name= "COM.QLF.BROADCAST.BROADCASTRECEIVEBYXML" >
<intent-filter>
<action android:name= "Com.qlf.broadCastFlag" >
</action>
</intent-filter>
</receiver>
The android:name in the receiver is the class that receives the broadcast in the program. The following intent-filter is similar to the function we talked about, and this action is the flag on top. Now that we run the program, we find that we can also implement the above function. The results of the operation are as follows:
In addition to using our own to send broadcasts, Android also has many broadcasts built into it. As we mentioned above, when the message comes, Android sends a broadcast with the action called "Android.provider.Telephony.SMS_RECEIVED." This time, if we want to accept this broadcast, you can receive message information by setting the action in the configuration file to the string above. Android includes many other broadcast action, with interested students searching the Internet. Here is no longer an example.