BroadcastReceiver is used to receive broadcast Intent. The sending of broadcast Intent is implemented by calling Context. sendBroadcast () and Context. sendOrderedBroadcast. Generally, a broadcast Intent can be received by multiple broadcast recipients subscribed to this Intent.
0. The method to implement a broadcast receiver is as follows:
(1) Step 1: Inherit BroadcastReceiver and override the onReceive () method.
Public class IncomingSMSReceiver extends BroadcastReceiver
{
@ Override public void onReceive (Context context, Intent intent ){}
}
(2) Step 2: subscribe to the broadcast Intent you are interested in. There are two subscription methods:
First: Subscribe using code
IntentFilter filter = new IntentFilter ("android. provider. Telephony. SMS_RECEIVED ");
IncomingSMSReceiver extends ER = new IncomingSMSReceiver ();
RegisterReceiver (receiver, filter );
Type 2: subscribe to the <application> node in the AndroidManifest. xml file:
<Cycler android: name = ". IncomingSMSReceiver">
<Intent-filter>
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
1. In addition to the arrival of text messages to broadcast Intent, Android also has many broadcast Intent types, such as startup, battery power change, and time change.
(1) receive the battery change broadcast Intent and subscribe to this Intent in the <application> node in the AndroidManifest. xml file:
<Cycler android: name = ". IncomingSMSReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BATTERY_CHANGED"/>
</Intent-filter>
</Cycler>
(2) receive the boot broadcast Intent and subscribe to this Intent in the <application> node in the AndroidManifest. xml file:
<Cycler android: name = ". IncomingSMSReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
And you need to declare the permission:
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
2. In Android, the response of a program is monitored by the system services Activity Manager and Window Manager. When BroadcastReceiver is not executed within 10 seconds, Android considers the program to be unresponsive. Therefore, you cannot perform some time-consuming operations in BroadcastReceiver. the ANR (Application No Response) dialog box is displayed on the other side. If you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. Instead of using sub-threads, because BroadcastReceiver has a short life cycle (the BroadcastReceiver instance will be destroyed after onReceive () is executed ), the sub-thread may have ended before it has ended. Of course, if BroadcastReceiver ends, its host process is still running, and the sub-thread will continue to execute. However, the host process is easily killed first when the system requires memory because it is a null process (a process without any active components ).
Public class IncomingSMSReceiver extends BroadcastReceiver {
@ Override public void onReceive (Context context, Intent intent ){
// Send Intent to start the service. The service performs time-consuming operations.
Intent service = new Intent (context, XxxService. class );
Context. startService (service );
}
}
Each time a message is broadcast, A BroadcastReceiver instance is created and the onReceive () method is executed.
From Cloay's column