Broadcastreceive Broadcast receivers:

Source: Internet
Author: User

Tag:android    use detailed    

Broadcastreceive Broadcast receiver: public class Test extends activity{private final String action_name = "Send Broadcast";            Private Button mbtnmsgevent = null;                    protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                    Registered broadcast Registerboradcastreceiver ();          LinearLayout mlinearlayout = new LinearLayout (this);          Mbtnmsgevent = new Button (this);          Mbtnmsgevent.settext ("Send broadcast");          Mlinearlayout.addview (mbtnmsgevent);                    Setcontentview (mlinearlayout);                  Mbtnmsgevent.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                  Intent mintent = new Intent (action_name);                                    Mintent.putextra ("Yaner", "send broadcast, equivalent to transmit data here");              Send broadcast Sendbroadcast (mintent);      }          }); } private Broadcastreceiver Mbroadcastreceiver = new Broadcastreceiver () {@Override public void onreceive (context context, Intent Intent) {Str              ing action = intent.getaction ();              if (Action.equals (Action_name)) {Toast.maketext (test.this, "process the broadcast corresponding to the action name", 200);            }          }                };          public void Registerboradcastreceiver () {Intentfilter myintentfilter = new Intentfilter ();          Myintentfilter.addaction (Action_name);      Registered broadcast Registerreceiver (mbroadcastreceiver, Myintentfilter);  }}//Remember to close the broadcast protected void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy (); Unregisterreceiver (receiver); Your app can use it to filter external events only to receive and respond to an external event of interest, such as when the phone is being called, or when the data network is available. The broadcast receiver does not have a user interface. However, they can start an activity or serice to respond to the information they receive, or use Notificationmanager to notify the user. Notifications can be used in a variety of ways to attract users ' attention-flashing back lights, shaking, playing sounds, and more. In general, a persistent icon is placed on the status bar, and the user can open it and get the message. Broadcast type: Normal broadcast, ordered broadcast via Context.sendbroadcast (Intent myintent), via Context.sendorderedbroadcast (Intent, receiverpermission), the 2nd parameter of the method determines the level of the broadcast, the level value is between 1000 and 1000, the higher the value, the higher the priority of the Send , the level at which the broadcast receiver receives the broadcast (which can be set to 2147483647 by the priority setting in the Intentfilter), the order received at the same level is random, and then to the low level of the broadcast, A high level or the same level of receiving a broadcast first can truncate the broadcast through the Abortbroadcast () method so that other receivers cannot receive the broadcast, as well as other constructors for asynchronous broadcasts, through Context.sendstickybroadcast (Intent Myintent), and Sendstickyorderedbroadcast (intent, Resultreceiver, Scheduler, Initialcode, Initialdata, Initialextras) method, the method has the characteristics of an ordered broadcast and an asynchronous broadcast; Send an asynchronous broadcast to: <uses-permission android:name= "android.permission.BROADCAST_ STICKY "/> permission, after receiving and processing the intent, the broadcast still exists until you call Removestickybroadcast (intent) Take the initiative to get rid of it. Note: The intent parameter when sending a broadcast differs from the intent that is initiated by contex.startactivity (), which can be called by multiple broadcast receivers that subscribe to it, which can only be accessed by one (activity or service) Call the Listen broadcast Intent step:1> write a class that inherits Broadcastreceiver, overriding the OnReceive () method, and the broadcast sink is active only when it executes the method. When OnReceive () returns, it is inactive, note: In order to ensure the smooth user interaction process, some time-consuming operations to put into the thread, such as the class name smsbroadcastreceiver2> register the broadcast receiver, registered there are two methods of program dynamic registration and Android The static registration in the Oidmanifest file (which can be understood as registration in the system) is as follows: Static registration, registered broadcast, the following priority indicates the level of receiving broadcast "2147483647" for the highest precedence <receiver android:name= ". SmsbRoadcastreceiver "><intent-filter android:priority =" 2147483647 "><action android:name=" Android.provider.Telephony.SMS_RECEIVED "/></intent-filter></receiver > Dynamic registration, The Broadcastreceiverintentfilter intentfilter=new intentfilter ("Onresume") is generally registered within the activity when interacting. Android.provider.Telephony.SMS_RECEIVED "); Registerreceiver (Mbatteryinforeceiver, intentfilter);// Anti-registration Unregisterreceiver (receiver); Note: 1. The life cycle is only about 10 seconds, if within onreceive () do more than 10 seconds of things, will report ANR (Application No Response) The program does not respond to the error message, if you need to complete a more time-consuming work, should be sent by Intent to the service, by the service to complete. It is not possible to use a child thread here because the Broadcastreceiver life cycle is short, and the child thread may not have finished broadcastreceiver before it ends. Once the broadcastreceiver is over, the broadcastreceiver process can easily be killed when the system needs memory because it belongs to the empty process (the process without any active components). If its host process is killed, then the working child thread will be killed. So using a sub-thread to solve is unreliable 2. The dynamic registration of the broadcast receiver also has the feature that when the activity used to register is turned off, the broadcast fails. Static registration does not need to worry about whether the broadcast receiver is turned off, as long as the device is turned on and the broadcast receiver is open. That is, even if the app itself does not start, the broadcast that the app subscribes to will act on it when it triggers. Common broadcast intent of the system, such as power-on startup, battery change, time change, etc.

Broadcastreceive Broadcast receivers:

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.