BroadCastReceiver-Reading Notes

Source: Internet
Author: User

16:55:07

1. BroadCastReceiver is one of the four Android components and is essentially a system-level monitor.

2. after each BroadCast event, the system creates the corresponding BroadCastReceiver object and automatically triggers its onReceive () method. This method must be executed within 10 seconds. Otherwise, ANR will appear. Therefore, if you need to complete a time-consuming task, you should start a Service. You cannot start a thread in BroadCastReceiver because the life cycle of BroadCastReceiver is very short, sometimes the sub-thread is not completed, but BroadCastReceiver has exited. If the process where BroadCastReceiver is located exits, although there are new threads started by the user, however, because the process does not contain any components, the system may give priority to terminate the process when resources are insufficient. This will cause the sub-thread to fail to run normally.

3. BroadCastReceiver has two registration methods: Dynamic Registration and static registration.

Dynamic Registration is registered in the Code as follows:

                          final String INTENT_STRING =                      MyBroadCastReceiver receiver =          IntentFilter filter =     }

Static registration is registered in the AndroidManifest file, as follows:

 <receiver android:name=>     <intent-filter >         <action android:name=/>     </intent-filter> </receiver>

4. Differences between the two registration methods:

4.1 static registration after the program exits (click back until it exits), the received broadcast can still be accepted and processed, but dynamic registration cannot (the Home key returns to the desktop ).

4.2 Once registered, static registration will always run on the system background, which is resource-consuming. Dynamic registration can be flexibly controlled using code and registered in onResume, cancel registration when the program exits onPause () or is invisible: unregisterReceiver (assumer );

4.3 If the mobile phone is restarted, neither static registration nor dynamic registration will be able to handle broadcast.

5. Three broadcast sending Methods: sendBroadcast (intent), sendStickyBroadcast (intent), and sendOrderedBroadcast (intent, null ).

5.1 sendBroadcast: Send normal broadcasts. If BroadCastReceiver is dynamically registered, once it exits, it cannot receive the broadcast sent in this way, but static registration can receive it.

5.2 For broadcasts sent by sendStickyBroadcast, the dynamically registered BroadCastReceiver can receive broadcasts when the Activity is onResume again.

5.3 The sendOrderedBroadcast () method is used to broadcast Ordered events (Ordered broadcast) to the system. the receiving sequence set in the xml file receives Intent sequentially. The receiving priority can be set in the system configuration file (declared in the android: priority attribute of the intent-filter element, the greater the value, the higher the priority level. The value range is-1000 to 1000. Of course, you can also set it by calling the setPriority () method of the IntentFilter object ). For ordered broadcast, the previous receiver can process the received broadcast Intent (Intent), place the processing result in the broadcast Intent, and then pass it to the next receiver, of course, the previous receiver has the right to terminate further broadcasting. If the broadcast is terminated by the previous receiver, the subsequent receiver will no longer be able to receive the broadcast. The Code is as follows:

Set priority:

 <intent-filter android:priority=>

Terminate the BroadCastReceiver propagation or add data and pass it to the next receiver:

  setResultExtras(Bundle);

The next receiver can retrieve the newly added data from the previous receiver:

 getResultExtras();

6. typical sendOrderedBroadcast applications: for example, if we want to filter text messages, we know that the BroadCast sent after the system receives the text message is OrderedBroadcast. Can I register a Receiver to intercept the text message BroadCast and process the text message content first, at the same time, you can filter out spam messages by canceling the spread of broadcasts.

 

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.