This lesson learns the last of four components, the broadcast recipient. As the name implies, the recipient receives the broadcast. In the real world, for example, every family had a radio, which could be used to receive messages from the broadcast. As we all know, the program world is also a reference to the display of life design, that in the Android system also introduced the concept of broadcasting. What's the role of broadcasting in Android? For example: If you are playing a game or watching a video suddenly the battery is too low, a box will pop up to remind you that your phone is too low, please charge the prompt. In fact, the power change is a type of broadcast, when the power is too low, the system will be a broadcast, the running program will be received, at the same time to remind users. There are many types of broadcasts in the Android system. For example: Boot, shutdown, call, send text messages, screen unlock and so on.
Before example, let's start by creating a broadcast recipient
The first step:
Create a Java class that I named Mybroadcastreceiver. Inherit Broadcastreceiver at the same time
Step two: Register in the manifest file (all four components need to be registered)
<receiver android:name= "Com.ui.broadcastreceiver.MyBroadcaseReceiver" ></receiver>
The third step: you need to configure you to accept that type of broadcast, the system is broadcast so much, you have to specify whether you want to accept the type of broadcast (such as we accept the battery low radio)
<receiver android:name= "Com.ui.broadcastreceiver.MyBroadcaseReceiver" > <intent-filter> < Action android:name= "Android.intent.action.BATTERY_LOW"/> </intent-filter> </receiver>
Fourth step: Specify the permission to accept the broadcast, which is not required for every broadcast. Generally violate the privacy of users need to add permissions, such as receiving SMS broadcast
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
In this way, a broadcast recipient is created successfully. When the system sends a broadcast that is just like the type you accept, the Onreceiver method of the broadcast we create will be called.
Now that we've learned how to create a broadcast recipient, we'll use the example to learn how to receive a broadcast from a system call, and when a user makes a call, we'll call our Onreceiver method if we register for a broadcast on a big phone.
Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.util.log;public class Mybroadcasereceiver extends Broadcastreceiver {//call @overridepublic void when receiving a broadcast to the system call OnReceive (Context arg0, Intent arg1) {//Get the phone number in the broadcast string # = Getresultdata (); LOG.I ("Mybroadcastreceiver", "there is a phone call out, the number is:" +number);}
Need to go to the manifest file to register the broadcast recipient, plus the monitoring of that ad
<receiver android:name= "Com.ui.broadcastreceiver.MyBroadcaseReceiver" > <intent-filter> < Action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver >
Note that you need to add phone privileges:
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
At this time we run the program after the call: The display effect is:
is not very simple, very studious. Compared to Activity,service and ContentProvider, radio recipients are very studious. But very studious does not mean useless, in fact, the four components are very useful.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components Learning Broadcastreceiver one