Android Development 22—— BroadcastReceiver Radio Receiver Principle and Registration Method

Keywords: Android xml Mobile

               


1. Basic concepts of broadcasting mechanism

When an event occurs, such as a text message or a phone call, the android operating system broadcasts the event to all registered broadcast recipients, who need to process the event.In fact, this is the broadcast in everyday life.When a news happens, the radio station broadcasts it to the person who turns on the radio. People interested in the news will pay attention to it and may take notes.News is an event, a radio station is an android system, the person who turns on the radio is the radio receiver, the person who is interested is the radio receiver who needs to deal with the event, and taking notes is the operation on the event.

 


2. Classification of broadcasting - General broadcasting and ordered broadcasting

(1) General broadcasting: Completely asynchronous, logically receivable by any broadcasting recipient.The advantage is high efficiency.The disadvantage is that the receiver cannot pass the processing result to the next receiver and cannot terminate the propagation of the broadcast intent.


(2) Ordered broadcasting: one transmission among the recipients in the priority order of the recipients.For example, there are three broadcast receivers A, B, C with priority A > B > C.That message goes to A, B, and C.Each recipient has the right to terminate the broadcast, such as B terminating the broadcast, C cannot receive it.In addition, when A receives a broadcast, it can operate on the result object. When the broadcast is passed to B, B can get data stored in A from the result object.If the system receives a broadcast from a short message, it is an orderly broadcast.

 


3. Two ways of registering broadcasters

1 Register in AndroidManifest.xml

The receiver registered in the profile is characterized by the fact that even if the application is closed, the receiver can still accept the broadcast it is interested in, such as the radio receiver of the battery power of the mobile phone, without having to turn on a program.Examples 1 and 2 below show that a radio receiver receives a broadcast that calls.

    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                 <!-- Broadcast Receiver 1-->  <receiver android:name=".BroadcastReceiver1">   <intent-filter>    <action android:name="android.intent.action.CALL"></action>   </intent-filter>  </receiver>    <!-- Broadcast Receiver 2 -->  <receiver android:name=".BroadcastReceiver2">   <intent-filter>    <action android:name="android.intent.action.CALL"></action>   </intent-filter>  </receiver>    <!-- Broadcast Receiver 3 -->  <receiver android:name=".BroadcastReceiver3">   <intent-filter>    <action android:name="android.intent.action.PICK"></action>   </intent-filter>  </receiver>      </application>
/** * Analog Call Broadcasting * * @author Xu Yue **/public class MainActivity extends Activity@Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  Intent intent = new Intent();  intent.setAction("android.intent.action.CALL");  this.sendBroadcast(intent); }}/** * Each broadcast received generates a new BroadcastReceiver1, which is no longer used when the onReceive method is processed * Receive again is generating a new BroadcastReceiver1 object * * @author Xu Yue **/public class BroadcastReceiver1 extends android.content.BroadcastReceiverpublic BroadcastReceiver1() {  Log.i("xy_Receiver", "construtor1"); } @Override public void onReceive(Context context, Intent intent) {  Log.i("xy_Receiver", "onReceive1"); }}/** * Broadcast Receiver 2 * * @author Xu Yue **/public class BroadcastReceiver2 extends android.content.BroadcastReceiverpublic BroadcastReceiver2() {  Log.i("xy_Receiver", "construtor2"); } @Override public void onReceive(Context context, Intent intent) {  Log.i("xy_Receiver", "onReceive2"); }}/** * Broadcast Receiver 3 * * @author Xu Yue **/public class BroadcastReceiver3 extends android.content.BroadcastReceiverpublic BroadcastReceiver3() {  Log.i("xy_Receiver", "construtor3"); } @Override public void onReceive(Context context, Intent intent) {  Log.i("xy_Receiver", "onReceive3"); }}

(2) Register in Activity

Binding a recipient in an Activity must depend on the application to exist, or a BroadcastReceiver is used to update the UI, so there is no need for the recipient to run when the program is closed, so it can be registered in the Activity without having to register in AndroidManifest.xml.

/** * Activity Registered Broadcast Receiver in * * @author Xu Yue **/public class MainActivity extends Activityprivate BroadcastReceiver receiver; private static final String CALL_ACTION = "android.intent.action.CALL"@Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main); } /**  * Analog broadcast to send a phone * * @param v  */ public void sendBroadCast(View v) {  Intent intent = new Intent();  intent.setAction("android.intent.action.CALL");  this.sendBroadcast(intent); } public void bindReceiver(View v) {  receiver = new BroadcastReceiver();  IntentFilter intentFilter = new IntentFilter();  intentFilter.addAction(CALL_ACTION);  this.registerReceiver(receiver, intentFilter); } public void unBindReceiver(View v) {  this.unregisterReceiver(receiver); }}public class BroadcastReceiver extends android.content.BroadcastReceiver@Override public void onReceive(Context context, Intent intent) {  Log.i("xy", "receiver"); }}


 

 


           

Posted by hbsnam on Wed, 01 May 2019 02:30:37 -0700