1. BroadcastReceiver (broadcast receiver)
This component accepts the broadcasted intent. Context can broadcast through the sendBroadcast () and sendOrderedBroadcast () methods.
Public class IncomingSMSReceiver extends BroadcastReceiver {
Public void onReceiver (Context context, Intent intent ){
}
}
Registered recipient
Programming (intention action name ))
Filter = new IntentFilter ("android. provider. Telephony. SMS_RECEIVED ");
IncomingSMSReceiver extends ER = new IcomingSMSReceiver ();
RegisterReceiver (receiver, filter );
Declarative
<Cycler android: name = ". IncomingSMSReceiver"
// The sender must have this permission.
Android: permission = "cn. android. permission. customer"
>
<Intent-filter>
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"> </action>
</Intent-filter>
</Cycler>
Ctx. sendBroadcast (intent, "permission string required by the recipient ");
After the mobile phone receives the text message, the android System Broadcasts an intent (the intent of receiving the text message), which is received by the recipient.
Public class IncomingSMSReceiver extends BroadcastReceiver {
// Android. provider. Telephony. Sms. Intents. SMS_RECEIVED_ACTION
String SMS_RECEIVED = "android. provider. Telephony. SMS_RECEIVED"; // action name
Public void onReceive (Context context, Intent intent ){
If (intent. getAction (). equals (SMS_RECEIVED )){
SmsManager sms = SmsManager. getDefault ();
Bundle bundle = intent. getExtras ();
Object [] pdus = (Object []) bundle. get ("pdus ");
SmsMessage [] messages = new SmsMessage [pdus. length];
For (int I = 0; I <pdus. length; I ++ ){
Messages [I] = SmsMessage. createFromPdu (byte []) pdus [I]);
For (SmsMessage message: messages ){
String msg = message. getMessageBody ();
Sms. sendTextMessage (to, null, msg, null, null );
}
}
}
}
}
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
<Uses-permission android: name = "android. permission. SEND_SMS"/>
Note: android. provider. Telephony. Sms. Intents. getMessageFromIntent (null );
Broadcast type
Normal broadcast (Normal broadcasts)
Asynchronous, received by many recipients at the same time, cannot pass the processing to the next receiver, and cannot terminate the broadcast.
Ordered broadcasts)
The broadcast is received in the order of priority of the receiver, and the priority level is declared in the priority in intent-filter. The value ranges from-1000 to 1000. The higher the priority level, the broadcast intention can be terminated, the recipient can tamper with the content.
<Intent-filter android: priority = "1">
<Action android: name = "com. android. frameworks... BROADCST_ABORT"/>
Context. sendBroadCast (intent );
// Specify the permission string to send the Broadcast
Context. sendBroadCast (intent, permissionStr );
Context. sendOrderedBroadCast ();
Aggreger. setResultExtra (bundle );
Cycler. getResultExtra (true );
Response of broadcast Receiver
Each time the broadcast arrives, the receiver object is re-created and the onReceive () method is called. After the execution is complete, the object is destroyed. When the onReceive () method is not executed within 10 seconds, Android considers the program to be unresponsive, so some time-consuming operations cannot be performed in BroadcastReceiver, otherwise, the ANR (Application No Response) dialog box is displayed.
If you need to complete a time-consuming task, you should send Intent to the Service, which is completed by the Service. The subthread cannot be used here because BroadcastReceiver has a short life cycle, the sub-thread may not end yet.
BroadcastReceiver is over. Once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory because it is a blank process (process without any active components ). If its host process is killed, the working sub-thread is also killed, so it is unreliable to use the sub-thread to solve the problem.
Public void onReceive (Context context, Intent intent ){
// Send Intent to start the service, and there is a service to complete time-consuming operations
Intent Service = new Intent (content, XxxService. class );
Context. startService (service );
}
<Action android: name = "android. intent. action. BATTERY_CHANGED"/> // power change
<Action android: name = "android. intent. action. BOOT_COMPLETED"/> // the startup is complete.
<Uses-permission www.2cto.com
Android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
Author: to1297488504