Android broadcast mechanism-1-Broadcast Mechanism in Android

Source: Internet
Author: User

Reading directory

I. What is BroadcastReceiver?

Ii. layout file writing

Iii. Code File writing

Iv. Compilation of project definition files

V. Running Effect

 I. What is BroadcastReceiver?

In real life, when we are driving, we will hear the traffic station say "Towards the inner street congestion" from the broadcast. For example, Please bypass the vehicles that pass through from here. This is the broadcast, the transport station is the sender, and we are the receiver. As for whether we are listening to the transport station, we may be listening to the crosstalk of Guo Degang, or even though we have listened to the transport station, it is our own business to move toward the neighborhood, so it is the same in Android. broadcast only sends data. As for how to receive and process data, it is the recipient's business.

For us, we first need to register the broadcast receiver class to the Android operating system to let the Android operating system know, now there is a broadcast receiver waiting to receive broadcasts from the Android system. When an event occurs in the Android operating system, such as receiving a call, the Android operating system will notify all BroadcastReceiver objects registered on it to tell them what an event is like, these BroadcastReceiver objects first determine whether this event is an event of interest to our receiver. Different BroadcastReceiver objects may process different events, I am processing it.

  Ii. layout file writing

Main. xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" 4 android: layout_width = "fill_parent" 5 android: layout_height = "fill_parent"> 6
7 <Button android: layout_width = "wrap_content" 8 android: layout_height = "wrap_content"
9 android: text = "Send broadcast"
10 android: id = "@ + id/br"/>
11 </LinearLayout>

Iii. Code File writing

3.1: MainActivity. java

1 package com. menglin. broadcastreceiver; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. view. view; 7 import android. view. view. onClickListener; 8 import android. widget. button; 9 10 public class MainActivity extends Activity11 {12 // declare a Button object btn13 private Button btn; 14 @ Override15 public void onCreate (Bundle savedInstanceState) 16 {17 super. onCreate (savedInstanceState); 18 // load the layout file main. xml19 setContentView (R. layout. main); 20 btn = (Button) findViewById (R.id.br); 21 // bind the listener to the Button and click event 22 btn. setOnClickListener (listener); 23} 24 25 // listener Click Event 26 private OnClickListener listener = new OnClickListener () 27 {28 @ Override29 public void onClick (View v) 30 {31 // create a new Intent object 32 Intent intent = new Intent (); 33 // set the Intent object action to call 34 intent. setAction (Intent. ACTION_CALL); 35 // send broadcast 36 sendBroadcast (intent); 37} 38 };
39}

3.2 BroadcastReceiver. java

This class is a class inherited from BroadcastReceiver. This class is used to handle broadcast events sent by the Android operating system. That is to say, the Android operating system sends an event broadcast. We are responsible for receiving this class and will call the onReceive () method after receiving it.

When will the life cycle of this BroadcastReceiver object end? After the onReceive () method is called, The BroadcastReceiver object ends, and the next time the broadcast is received, the new BroadcastReceiver object is generated.

1 package com. menglin. broadcastreceiver; 2 3 import android. content. context; 4 import android. content. intent; 5 import android. util. log; 6 7 public class BroadcastReceiver extends android. content. broadcastReceiver 8 {9 private static final String Tag = "BroadcastReceiver"; 10 // constructor 11 public BroadcastReceiver () 12 {13 Log. I (Tag, "constructor"); 14} 15 16 // override onReceive () method 17 public void onReceive (Context context, Intent intent) 18 {19 Log. I (Tag, "received"); 20} 21}

Iv. Compilation of project definition files

AndroidMainfest. xml

To enable the BroadcastReceiver class to receive events sent by the Android operating system, you must register the BroadcastReceiver class to the Android operating system. This part of red code is required, the <intent-filter/> node determines the type of events that the BroadcastReceiver class should receive. <intent-filter/> is the meaning of the filter, so that you can pass the criteria, if it does not comply with the standard, you will not be allowed to pass. That is to say, we want to notify a broadcast receiver BroadcastReceiver that the Intent object needs to be sent. If the ACTION of the Intent object complies with <action android: name = "android. intent. action. CALL "/> to start the BroadcastReceiver. This node is defined here for what events our broadcast receiver receives.

 1 <?xml version="1.0" encoding="utf-8"?> 2   <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3       package="com.menglin.broadcastreceiver" 4       android:versionCode="1" 5       android:versionName="1.0"> 6     <uses-sdk android:minSdkVersion="8" /> 7  8       <application android:icon="@drawable/icon" android:label="@string/app_name"> 9           <activity android:name=".MainActivity"10                   android:label="@string/app_name">11               <intent-filter>12                   <action android:name="android.intent.action.MAIN" />13                   <category android:name="android.intent.category.LAUNCHER" />14               </intent-filter>15           </activity>16       <receiver android:name=".BroadcastReceiver">17                <intent-filter>18                    <action android:name="android.intent.action.CALL" />19                </intent-filter>20           </receiver>21       </application>22   </manifest>

  V. Running Effect

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.