Joke Collection Website - Public benefit messages - Brief introduction of android broadcasting mechanism

Brief introduction of android broadcasting mechanism

In Android, after some operations are completed, a broadcast will be sent, such as texting or making a phone call. If a program receives this broadcast, it will handle it accordingly. This broadcast is somewhat similar to our traditional radio broadcast. It is called broadcasting because it is only responsible for "speaking", whether you listen or not, that is, no matter how your receiver handles it. In addition, the broadcast can be received by multiple applications, and of course it may not be received by any application.

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.