Joke Collection Website - Public benefit messages - Brief introduction of android broadcasting mechanism
Brief introduction of android broadcasting mechanism
The biggest feature of broadcast mechanism is that the sender doesn't care whether the receiver receives the data or how the receiver handles the data.
Android will broadcast various events generated in the operating system. For example, receiving a short message will generate an event to receive the short message. Once the Android operating system generates these events internally, it will broadcast them to all broadcast receiver objects.
1. 1 broadcast receiver
BroadcastReceiver is a component provided for system broadcasting, and the broadcast event handling mechanism is system-level. For example, we can send a broadcast to test whether we have received a short message. At this time, we can define a BraodcastReceiver to receive the broadcast and prompt the user when receiving the short message. We can use Intent to start a component, or we can use the sendBroadcast () method to start a system-level event broadcast to deliver messages.
We can also develop a BroadcastReceiver in our own application, and then register the class or object of the BroadcastReceiver into the Android operating system, so that the operating system can know that there is such a broadcast receiver waiting to receive the broadcast of the Android operating system, that is, we can implement the broadcast receiver in our own application to listen and respond to the broadcast intention.
When a broadcast event happens, the Android operating system first tells the broadcast receiver registered on it what kind of event happened, and each receiver first judges whether it is an event that my receiver needs, and if it is, it will handle it accordingly.
For example, when we put the blacklist of harassing calls into the database, when we receive a call, we will generate a call-answering event. We will register a BroadcastReceiver object in the Android operating system in advance, and we will notify our broadcast receiver object when the event occurs. After receiving the message, the receiver object will go to the database to compare all the blacklisted phones with the received phone numbers, and if they match, hang up directly.
1.2 method of registering a broadcast receiver
BroadcastReceiver is used to listen to broadcast events (Intent). In order to achieve this goal, you must register the BroadcastReceiver. There are two ways to register:
1. Static registration
The method of static registration is to define the receiver in the application of AndroidManifest.xml and set the action to be received.
Characteristics of static registration: whether the application is active or not, it will be monitored.
& lt receiver Android: name = myreceiver >
& lt intention filter & gt
& ltAction Android:name = my receiver _ Action/& gt;
& lt/intent-filter & gt;
& lt/receiver & gt;
Among them, MyReceiver is a class that inherits the BroadcastReceiver, and rewrites the onReceiver method to handle the broadcast in the onReceiver method. The< Intention Filter & gt tag sets a filter to receive the specified action broadcast.
2. Dynamic registration
The dynamic registration method calls the function in the activity to register, similar to static content. One parameter is receiver and the other is IntentFilter, where is the action to be received.
Characteristics of dynamic registration mode: After registration in the code, when the application is closed, it will no longer be monitored.
my receiver receiver = new my receiver();
//Create a filter and specify an action for receiving broadcasts with the same action.
intent filter filter = new intent filter(my receiver _ Action);
//Register the broadcast receiver
RegisterReceiver (receiver, filter); //Specify the broadcast target action
Intent Intent = new Intent(my receiver _ Action);
//Messages can be delivered by intention.
intent . put extra(msg);
//Send a broadcast message
SendBroadcast (intention); //Log off the broadcast receiver
UnregisterReceiver (receiver);
note:
1. Generally, BroadcastReceiver is registered in onStart and cancelled in onStop.
2. The 2.BroadcastReceiver object is only valid when onReceive(Context, Intent) is called. When returned from this function, the object is invalid and the life cycle ends.
- Previous article:How do tourism enterprises use mail for marketing?
- Next article:Why did you write qq to him after receiving the inexplicable text message?
- Related articles
- What is the failure of downloading detailed tips on employee payment on Anhui medical insurance platform?
- Is "Operation Broken Card" really coming?
- What's the penalty decision number?
- Bless my nephew in the Spring Festival of 2022; Greetings and recommendations to my nephew in the Spring Festival of 2022.
- (Taiyuan) What's the difference between a random pass and a pass?
- Can you believe the dunning message starting with 86? I haven't decided yet.
- Will there be a SMS notification after the housing provident fund is withdrawn?
- How to get a refill for a pen-writing composition for free
- Xiaomi mobile phone can't be unsealed. What if the text message can't be sent out?
- How much do you charge for sending short messages in groups?