[Android] dynamically registers a broadcast Receiver

Source: Internet
Author: User

In essence, the broadcast mechanism of the Android system is a message subscription/publishing mechanism. Therefore, the first step to use this message-driven model is to subscribe to messages. for Android applications, subscribing to a message is actually registering a broadcast receiver.

Two registration methods are available: static registration and dynamic registration. In the Broadcast Mechanism of Android, the priority of Dynamic Registration is higher than that of static registration. Therefore, when necessary, we need to dynamically register the broadcast receiver.

Let's review static registration. The so-called registration is to register the broadcast receiver in manifest. xml.

 <receiver android:name="" >            <intent-filter android:priority="2147483647" >                <action android:name="android.provider.Telephony.SMS_RECEIVED" >                </action>            </intent-filter>        </receiver>

Then create a class that inherits broadcastreceiver. Specifically, the priority in the intent-filter label sets the priority of the broadcast receiver. Many data on the Internet shows that the priority value is set to 1000 ~ -2147483647, is the largest, but in fact, when the priority value is the maximum integer value, it is the highest priority, that is,; of course, the "highest" is limited to static registration.

For more information about static registration, see [Android] Text Message application-real-time acquisition of text message information. The acquisition of text message information is the use of static registration of broadcast receivers.

Next, let's go back to the question: how to dynamically register a broadcast receiver.

First, let's write an internal class that inherits broadcastreceiver.

private BroadcastReceiver myReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context, "myReceiver receive", Toast.LENGTH_SHORT).show();}};

In onreveice, we write a toast to check whether the broadcast receiver is successfully registered later.

Let's take a look at how to register a broadcast receiver.

private static final String ACTION = "cn.etzmico.broadcastreceiverregister.SENDBROADCAST";
IntentFilter filter = new IntentFilter();filter.addAction(ACTION);filter.setPriority(Integer.MAX_VALUE);registerReceiver(myReceiver, filter);

We also set the priority during registration. As mentioned above, we use the maximum integer value to obtain the highest priority in the broadcast mechanism.

After the broadcast registration is completed, the following is a test to check whether the broadcast receiver is successfully registered.

sendBroadcast(new Intent(ACTION));

Another feature of Dynamic Registration of broadcast receivers is that when the activity used for registration is disabled, the broadcast will become invalid. It also reflects the advantage of static registration, that is, there is no need to worry about whether the broadcast receiver is disabled, as long as the device is enabled, the broadcast receiver is turned on.

However, we can dynamically register the broadcast receiver in the service and set it to auto-start upon startup, so that we will not worry about closing like activity. However, if you want to use software such as "self-starting Butler" to manage boot self-starting programs, the broadcast receiver cannot be successfully registered if the relevant self-starting program is disabled. To solve this problem, it depends on the source code to determine whether the program's self-starting function can be disabled. In addition, is there any program in the current market that can manage the broadcast receiver to disable the static registration broadcast receiver? I am still investigating and haven't found out yet. If you know anything, please leave a message ~ Thank you!

In subsequent articles, I will write an article about implementing the highest priority text message interception function by dynamically registering the broadcast receiver method. The article will mention how to enable auto-start service and dynamically register broadcast receivers with service. Stay tuned! Thank you ~

PS: there was a small bug in the previously attached resources, which caused inconvenience. Sorry !!! The following link is newly uploaded ...... No bugs ...... I promise!

Engineering Resources: http://download.csdn.net/detail/etzmico/4116111

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.