Measure the test taker's understanding about Viscous broadcast in Android.

Source: Internet
Author: User

Original article: http://blog.csdn.net/weidi1989/article/details/8016688

BroadcastreceiverAs a broadcast receiver, because the transmission of messages between Android components is based on intent, what type of broadcast the broadcast receiver wants to receive, set the value of the action tag under the intent-filter tag under the Consumer er tag to the broadcast type, as shown in figure

  1. <Cycler Android: Name = ". incomingsmsreceiver">
  2. <Intent-filter>
  3. <Action Android: Name = "android. Intent. Action. boot_completed"/>
  4. <Action Android: Name = "android. provider. telephony. sms_received"/>
  5. </Intent-filter>
  6. </Cycler>


The above Code actually registers two types of Broadcast Reception. When the system starts up, the broadcast and the arrival of the SMS broadcast (note that the SMS reception permission is added) will be received, then you can write the code you want to write in the onreceive () method.
To raise an extra question, you must add the permission question. In the current logcat, the permission denied prompt is not red, and it is changed to orange. I thought this was okay, after searching for the code for half a day, I found that the permission was assigned incorrectly. I wanted to block the phone number, but I couldn't get the data if I didn't have the permission. I found it for ten minutes. Although I have made more progress on the permission issue for the previous morning, I still think that if I have to find a problem with the android permission for more than ten minutes, it would be too much.
In addition to configuration in the configuration file, you can also subscribe

  1. Intentfilter filter = new intentfilter ("android. provider. telephony. sms_received ");
  2. Worker er worker ER = new worker Er ();
  3. Registerreceiver (receiver, filter );


Receiver is the class you write that inherits from broadcastreceiver. Intentfilter corresponds to action.
Another detail is the three sending methods of sendbroadcast.
Sendbroadcast (), sendorderedbroadcast (), and sendstickybroadcast ()

Sendbroadcast ()The broadcast of this method can be sent to all broadcast recipients in the order of registration. If you set the priority of the broadcast receiver at this time, if the priority is exactly the same as the registration order, there will be no problems. If the order is different, an exception such as leaked intentreceiver will occur, and the previous broadcast receiver cannot call the abortbroadcast () method to terminate it. If it calls broadcastreceiver trying
Return result during a non-ordered broadcast exception. Of course, the receiver that receives the broadcast can modify the broadcast data.

Sendorderedbroadcast ()The method, as its name implies, is that the attribute of priority can work, and the receiver before the queue can terminate the broadcast at any time. In addition, this API can specify the final receiver. This receiver is the last receiver that receives the broadcast time, and will certainly receive broadcast events, which cannot be intercepted by the previous receiver. The actual experiment is like this. Suppose I have three schedulers in order, and the sendorderedbroadcast () method specifies a finalcycler, the order in which intent passes to the four receivers is receiver1 --> finalverer --> receiver2 --> finalverer --> receiver3 --> finalverer. This feature can be used to count the number of schedulers that can listen to a broadcast in the system.

Sendstickybroadcast ()The literal meaning is to send a sticky broadcast. Using this API requires permission for Android. manifest. permission. broadcast_sticky: The sticky broadcast feature that intent will be retained until the broadcast event ends, and this broadcast has no so-called 10-second limit, the 10-second limit refers to the normal broadcast. If the onreceive method is executed for too long, the system will set the broadcast to the candidate that can be killed when the system resources are insufficient, the broadcast will be killed so that it will not be executed.
The following describes the life cycle and details of a broadcast receiver:
1. the life cycle of the broadcast receiver is very short. It is created when the broadcast is received and destroyed after the onreceive () method ends.
2. Do not perform time-consuming work in the broadcast receiver; otherwise, the application no response error dialog box will pop up.
3. It is best not to create a sub-thread in the broadcast receiver for time-consuming work, because after the broadcast receiver is destroyed, the process becomes an empty process and is easily killed by the system.
4. It is best to put the time-consuming work in the service for completion.
As for receiving users' text messages and implementing ipdialing, it is easy to understand the above.

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.