Illustration of Android Broadcast Mechanism

Source: Internet
Author: User

Understanding broadcast mechanisms in real life

When we hear the broadcast, we will first think of the village broadcast when we were a child. Every activity in the village is broadcast. Listening to a radio is also a kind of broadcast. There are many radio stations in the radio, and the content played by each radio station is different. Broadcast when receiving (Sender) Does not care about us (Receiver. For example, when we listen to the broadcast of the traffic station, the TV station tells us how we are doing in traffic conditions, but it does not care about how we handle the broadcast. This is not a question that the broadcast should be concerned about, okay. Here we have understood broadcast from some small examples in our lives. How do we operate broadcast in Android?

Broadcast Mechanism of Android

There are a variety of broadcasts in Android, such as battery usage, telephone reception, and SMS reception.ProgramDevelopers can also listen to these broadcasts and process program logic. Below I will draw a rough picture to help you understand the broadcast operating mechanism.

Android has a variety of broadcasts. Various broadcasts run in the Android system. When the system/application program is running, various broadcasts are registered with Android, when Android receives a broadcast, it will determine which broadcast events are required, and then register the events with applications with different events, different broadcasts may process different events or the same broadcast events. In this case, the android system needs to screen for us.

Case Analysis:

A classic phone blacklist first stores the blacklist number in the database. When a call arrives, we receive the incoming broadcast and match the blacklist number with a certain data in the database, if yes, handle the problem, such as phone disconnection or mute...

 

Demo analysis:

The following uses a demo to explain how to write broadcast in Android. In the demo, we set a button to set the button to click the listener and then click send broadcast, the background receives the broadcast and prints the log information.CodeAs follows:

 

Broadcastactivity Page code

Public   Class Broadcastactivity Extends Activity {
Public   Static   Final String action_intent_test =   " Com. Terry. Broadcast. Test " ;

/** called when the activity is first created. */
@ override
Public void oncreate (bundle savedinstancestate) {
super . oncreate (savedinstancestate);
setcontentview (R. layout. main);
button BTN = (button) findviewbyid (R. id. button01);
BTN. setonclicklistener ( New onclicklistener () {

@ override
Public void onclick (view V) {
// todo auto-generated method stub
intent = New intent (action_intent_test );
sendbroadcast (intent);
}< BR >});
}< BR >}

 

 

The receiver code is as follows:

 

Mybroadcast Receiver

Public   Class Mybroadcast Extends Broadcastreceiver {


PublicMybroadcast (){
Log. V ("Broadcast_tag","Mybroadcast");
}

@ Override
Public VoidOnreceive (context, intent ){
//Todo auto-generated method stub
Log. V ("Broadcast_tag","Onreceive");
}

}

 

Android broadcast Lifecycle

In the above receiver, the receiver inherits broadcastreceiver, overwrites its onreceive, and constructs a function. The following figure shows the life cycle of Android broadcast step by step. When I click the button, it sends a broadcast to Android, such:

Then, click the button to send a broadcast to the Android system. The log information is as follows:

Next I will draw an image to describe the broadcast life cycle in Android. Secondly, it is not as complex as activity. The operating principle is as simple:

Let's take a look at the explanation provided by the SDK:

If a broadcast finishes onreceive processing, the system determines that this object is no longer an active object and will finished it.

At this point, you should be able to understand the concept of broadcast lifecycle in Android, and do not need to introduce the code much. It is a very simple demo of sending broadcast and processing broadcast.

 

How does Android determine and filter broadcasts?

As we have mentioned before, Android broadcast has a variety of types. How does the Android system help us handle the broadcast we need and provide the corresponding broadcast service for us? Note that each implementation of a broadcast receiving Class must explicitly indicate which class needs to be broadcast in manifest of our application and set a filter for it, for example:

Tip: Action indicates an action to be executed. There are many actions in andriod, such as action_view and action_edit.

 

 

Some may ask, what if I want to process multiple actions in a broadcast receiver? What should we do? In the android receiver, onreceive is taken as our thought. Similarly, you must register this action in intent-filter. It can be either a system broadcast action or a broadcast you need, then you need to use intent. getaction () can be used to determine the passed action to perform different processing and actions. For more information, see test.

 

 



Summary:

 

    • In Android, if you want to send a broadcast, you must use sendbroadcast to send a broadcast receiver to the system that is interested in it.
    • To use broadcast, you must have an intent object that must set its action object.
    • To use broadcast, you must explicitly specify the broadcast object in the configuration file.
    • Each time a broadcast is received, a new broadcast receiving object is generated.
    • Do not handle too many logic problems in broadcast. It is recommended that complicated logic be handled by activity or service.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.