A blog thoroughly understand the Android broadcasting mechanism

Source: Internet
Author: User

Starting in http://blog.csdn.net/pwiling/article/details/47682413, no reprint

Several times before in the program encountered Android broadcast, always have this question: often in the broadcast activity inside the dynamic registration, dynamic registration must get Broadcastreceiver object, since we have obtained the object of the broadcast receiver, why also send broadcast, Directly with the broadcast receiver of the object calls inside the relevant logical processing method, no? Why do you have to go around such a big circle with the radio?
It used to be a quick tour, and this time it was clear. Before we answer this question, let's start by learning more about the Android broadcasting mechanism

What is the Android broadcast mechanism

After some operations in Android are completed, a broadcast is sent, and if a program receives a broadcast, it will handle it accordingly. Some are similar to method invocations, but also very different from method calls, because the method call, if the called method does not exist, the compiler will error, and the most important feature of the Android broadcast mechanism is to send, regardless of whether it is received, regardless of how the receiver handles the data

Three elements of the Android broadcast mechanism

Broadcast (broadcast)-for sending broadcasts;
Broadcast Receivers (Broadcastreceiver)-for receiving broadcasts;
Intent content (Intent)-a medium for saving broadcast-related information.
Here is a detailed description of these three elements

Broadcastreceiver

We inherit from the Broadcastreceiver class and rewrite the Onreceiver method so that we can customize a broadcast receiver in our program. When a broadcast event occurs, the Android operating system first tells the Register to its above the broadcast receiver to produce a kind of event, each receiver first to determine is not my this receiver needs the event, if it is required by the event, and then call its Onreceiver method, In this case, the related logical processing.
Broadcastreceiver is used for monitoring broadcast events, in order to achieve this, broadcastreceiver must register, the method of registration has the following two kinds:

Static registration
The static registration method is to define receiver in the Androidmanifest.xml application and set the action to be received.
The static sign-up method is a feature that listens regardless of whether the application is active or not.
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="MyReceiver_Action"/>
</intent-filter>
</receiver>

Where Myreceiver is the class that inherits the Broadcastreceiver, the Onreceiver method is overridden, and the broadcast is processed in the Onreceiver method. tag to set the filter to receive the specified action broadcast.

Dynamic registration
The dynamic registration method calls the function to register in the activity. One parameter is receiver and the other is Intentfilter, which is the action to receive.
Dynamic Registration Method Features: After registering in code, when the application is closed, no longer listens.
MyReceiver receiver = new MyReceiver();
//创建过滤器,并指定action,使之用于接收同action的广播
IntentFilter filter = new IntentFilter("MyReceiver_Action");
//注册广播接收器
registerReceiver(receiver, filter);

This is where the question arises: it is often in the activity to send the broadcast dynamic registration, dynamic registration must be obtained Broadcastreceiver object, since we have obtained the object of the broadcast receiver, why also send the broadcast, Directly with the broadcast receiver of the object calls inside the relevant logical processing method, no? Why do you have to go around such a big circle with the radio?
Dynamic registration also after sending out the broadcast, logout broadcastreceiver

unregisterReceiver(receiver);

Intent

new Intent("MyReceiver_Action");// 可通过Intent携带消息intent.putExtra("msg""发送广播");

Intent generally includes two aspects of the content, the specified action and the relevant information to carry. The action wants to be the address of a letter, the Android system only knows this "address", in order to send "letter" to the broadcast receiver. The information that intent carries is what we want to send.

Send broadcast

sendBroadcast(intent);

OK, let's introduce the Android broadcast mechanism, we can solve the problem at the beginning.

In fact, the Android broadcast mechanism is designed to address cross-process communication .

Memory cannot be shared between applications in the Android system. Therefore, interacting data between different applications (cross-process communication) is a bit trickier. There are two types of Android broadcasts, one within the program and one between programs. The broadcast in the program doesn't really make sense, for example, notification of time zone changes, low battery power, taking a photo or changing language options, which are the real purpose of the broadcast. Android broadcasts are typically used by one program to tell another program that some information has changed, changed, and needs to respond to it, regardless of which programs receive it.

If this blog answers some of your doubts, point to a top bar!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A blog thoroughly understand the Android broadcasting mechanism

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.