Joke Collection Website - Blessing messages - What is the situation when the onrecive method in broadcast in android is continuously executed in a loop?
What is the situation when the onrecive method in broadcast in android is continuously executed in a loop?
Introduction to BroadCastReceiver (source code at the end)
BroadCastReceiver source code is located at: framework/base/core/java/android.content.BroadcastReceiver.java
Broadcast receiver (BroadcastReceiver) is used to receive broadcast Intent. The sending of broadcast Intent is achieved by calling Context.sendBroadcast() and Context.sendOrderedBroadcast(). Usually a broadcast Intent can be received by multiple broadcast receivers subscribed to this Intent.
Broadcast is a widely used mechanism for transmitting information between applications. BroadcastReceiver is a type of component that filters, receives and responds to sent broadcasts;
Comes from ordinary applications, such as an application notifying other applications that certain data has been downloaded.
BroadcastReceiver itself does not implement a graphical user interface, but when it receives a notification, BroadcastReceiver can start an Activity in response, or remind the user through NotificationMananger, or start a Service, etc.
The mechanism of BroadCastReceiver
1. Mechanism
There are various broadcasts in Android, such as battery usage status, phone reception and text message reception. Each reception will generate a broadcast, and application developers can also monitor these broadcasts and process program logic.
As shown in the figure:
2. Implementation
Use receiving text messages as an example:
The first method:
Implementation
public class MyBroadcastReceiver extends BroadcastReceiver {
// action name
String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED" ;
public void onReceive( Context context, Intent intent) {
if (intent.getAction().equals( SMS_RECEIVED )) {
// Related processing: region change, low battery, incoming calls and messages;
}
}
}
System registration: Register in AndroidManifest.xml
lt; receiver android :name = ".MyBroadcastReceiver" gt;
lt; intent-filter android: priority = "1000" gt;
lt; action android: name = " android.provider.Telephony .SMS_RECEIVED" /gt;
lt;/ intent-filter gt;
lt;/receiver gt; Of course, permissions are required:
lt; uses- permission android: name = "android.permission.RECEIVE_SMS" /gt;
lt; uses-permission android: name = "android.permission.SEND_SMS" /gt;
Second Method:
//Broadcast receiver - broadcast reception
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Related processing, such as receiving text messages and monitoring power change information
}
};
Register in code:
IntentFilter intentFilter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED " );
registerReceiver( mBatteryInfoReceiver , intentFilter);
p>
3. Life cycle
Describes the life cycle of broadcast in Android. Secondly, it is not like
Activity is just as complex, and its operating principle is very simple as shown below:
The life cycle is only about ten seconds. If you do something for more than ten seconds within onReceive(), an error will be reported.
Each time a broadcast arrives, the BroadcastReceiver object will be re-created and the onReceive() method will be called. After execution, the object will be destroyed. When the onReceive() method is not executed within 10 seconds, Android It will be considered that the program is unresponsive. Therefore, some time-consuming operations cannot be performed in
BroadcastReceiver, otherwise the ANR (Application No
Response) dialog box will pop up.
- Related articles
- How to query the monthly income details of personal social security card?
- Send flowers to girls you like. What should I write on the card?
- How to download the weather app that comes with Apple's mobile phone after it has been deleted?
- Analysis of Mobile Gateway Receiving Super Long Short Messages
- Blessing words for an early recovery (28 selected sentences)
- 2021 Yantai colleges and universities return to school schedule for the fall semester
- What platform is the number1069092913119?
- How to lose weight by SMS?
- My mobile phone is Nokia N79. I wear headphones to listen to the song, send a short message, and the song will be played publicly! How can I make it unfair?
- Why can't I see WeChat when oppoA93 plays music?