Preface
1.1 In the previous lesson, we talked about how to register a BroadcastReceiver broadcast receiver in the project definition file AndroidManifest. xml. Today we will introduce how to register a BroadcastReceiver broadcast receiver in the code. In the project definition file AndroidManifest. if you register a BroadcastReceiver broadcast receiver in xml, even if your application has been disabled, the BroadcastReceiver broadcast receiver still receives the broadcast. For example, your application listens for photo events, after taking a picture, The BroadcastReceiver broadcast receiver will pop up a prompt box "you have taken an image, the picture has been saved". When you open this application, you can receive the broadcast, which is no problem, however, if you close this application, you can still receive the broadcast. You still need to pop up "you have taken the picture, the picture has been saved", which is not what we need in some scenarios, therefore, we need to use another method to register the BroadcastReceiver broadcast receiver.
1.2 When we know what is going on in Android, we need to know the BroadcastReceiver Actions built in Android so that we can control the events in Android. I will not write it here. You can refer to the Android SDK which is comprehensive and has instructions.
Ideas
BroadcastReceive is used to listen to broadcast events, which are broadcast from Intent objects. Therefore, we must register the BroadcastReceive class in our Android system. Today we will introduce how to register a BroadcastReceiver broadcast receiver in the code.
Steps
I. layout file writing
1.1: Layout file main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "binding broadcast receivers"
Android: id = "@ + id/start"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "unbinding broadcast receiver"
Android: id = "@ + id/end"
/>
</LinearLayout>
Ii. Code File writing
2. 1: MainActivity. java
Package com. menglin. broadcastreceiver;
Import android. app. Activity;
Import android. content. Intent;
Import android. content. IntentFilter;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class MainActivity extends Activity
{
Protected static final String SMS_Action = "android. provider. Telephony. SMS_RECEIVED ";
// Declare two Button objects
Private Buttonbtn_start, btn_end;
// Declare a custom BroadcastReceiver Class Object
BroadcastReceiver br;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// Load the layout file main. xml
SetContentView (R. layout. main );
// Use findViewById () to obtain two button objects
Btn_start = (Button) findViewById (R. id. start );
Btn_end = (Button) findViewById (R. id. end );
// Bind the two buttons to the listener and click the event
Btn_start.setOnClickListener (btn_start_listener );
Btn_end.setOnClickListener (cannellistener );
}
// Listen to the Click Event
Private OnClickListener btn_start_listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// Create a BroadcastReceiver object
Br = new BroadcastReceiver ();
// Create an IntentFilter object
IntentFilter intentfilter = new IntentFilter ();
// Add an Action for the IntentFilter object, which is also equivalent to the Action in AndroidManifest. the same as the xml definition. The purpose of the definition is to filter the messages generated by the Android operating system. We can only customize the BroadcastReceiver class to receive the short message broadcast, so that we can further process the code.
Intentfilter. addAction (SMS_Action );
// Register the broadcast Receiver
RegisterReceiver (br, intentfilter );
}
};
// Listen to the Click Event
Private OnClickListener btn_end_listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// Cancel the registered broadcast Receiver
UnregisterReceiver (br );
}
};
}
2.2 BroadcastReceiver. java
Package com. menglin. broadcastreceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. util. Log;
Public class BroadcastReceiver extends android. content. BroadcastReceiver
{
Private static final String Tag = "BroadcastReceiver ";
// Constructor
Public BroadcastReceiver ()
{
Log. I (Tag, "constructor ");
}
@ Override
Public void onReceive (Context context, Intent intent)
{
Log. I (Tag, "received ");
}
}
Iii. Project definition file
AndroidMainfest. xml
Add the permission node <uses-permission android: name = "android. permission. RECEIVE_SMS "/>, which was previously written as <uses-permission android: name =" android. permission. SMS_RECEIVE "/>, the system always reminds me permission denial: copying ing intent {act = intent
Android. provider. Telephony. SMS_RECEIVED}, I am still wondering, what is going on, isn't the permission added? Finally, check the SDK and find that the two words are reversed.
Execution Process
However, after clicking the "register broadcast receiver" button, we send a short message to the simulator through the "Emulator Control" Panel. At this time, our Log will output "received ", this "received" is output in our custom broadcast receiver class. After we click the "cancel broadcast receiver" button again, once again, we send a short message to the simulator through the "Emulator Control" Panel, and the Log will not be "received.
Operating Mechanism
When we register the broadcast receiver class we wrote to the Android operating system, the system will automatically call the BroadcastReceiver we wrote after receiving the short message. the onReceive () method in the java class. After the code in the onReceive () method is processed, the life of the broadcast receiver object will be destroyed. The BroadcastReceiver we wrote will not be called when the system takes a photo or receives a broadcast of an event such as a phone call. the onReceive () method in the java class, because we have written intentfilter. addAction (SMS_Action); this line of code indicates that our broadcast receiver is only interested in the broadcast of system events when the system receives short messages.
The running effect is as follows: