Introduction to Android Early-learning-broadcast receiver 02__android

Source: Internet
Author: User

Original address: http://blog.csdn.net/zuolongsnail/article/details/6450156

The following is an overview of Broadcastreceiver in Android Doc:
The ① broadcast receiver is a component that focuses on receiving broadcast notification information and making corresponding processing. Many broadcasts originate from system code-for example, notifying time zone changes, low battery power, taking a picture, or changing the language option for the user. Applications can also broadcast--for example, notifying other applications that some data downloads are complete and available.
The ② application can have any number of broadcast sinks to respond to all of the notification information that it is interested in. All sinks inherit from the Broadcastreceiver base class.
The ③ broadcast receiver does not have a user interface. However, they can initiate an activity to respond to the information they receive, or use Notificationmanager to notify the user. Notifications can be used in a number of ways to attract users ' attention-flashing back lights, shaking, playing sound, and so on. In general, a persistent icon is placed on the status bar, and the user can open it and get the message.

There are two kinds of broadcast events in Android, one is System broadcast events, such as: action_boot_completed (triggered after system startup), action_time_changed (triggered when system time changes), Action_battery_ Low (triggered when low-power) and so on. The other is our custom broadcast event.

the process of broadcasting events
① Registered Broadcast event: There are two ways to register, one is static registration, is defined in the Androidmanifest.xml file, the registered broadcast receiver must inherit Broadcastreceiver, the other is dynamic registration, is registered in the program using CONTEXT.REGISTERRECEIVER, registered broadcast receiver equivalent to an anonymous class. Both ways require intentfilter.
② sends broadcast events: sent by Context.sendbroadcast, intent to pass the action used when registering.
③ Receive broadcast event: When a broadcast is heard by the receiver, its onreceive () method is invoked and the intent object containing the message is passed to it. Do not exceed 5s of code execution time in OnReceive, or Android will eject timeout dialog.

below I demonstrate the use of custom broadcast events and system broadcast events through code. Full code Download address:Android_broadcastreceiver.rar

STEP1: Registers the broadcast event in the Mainactivity OnStart method. The static registration method is in the Androidmanifest.xml file.

STEP2: Clicking the corresponding button will trigger the appropriate way to send the broadcast message.

[Java]  View plain  copy/**   * MainActivity   *  @author  zuolongsnail  & nbsp;*   */   public class mainactivity extends activity {        private Button sendStaticBtn;        private button senddynamicbtn;       private Button  sendsystembtn;       private static final string staticaction  =  "Com.byread.static";       private static final  string dynamicaction =  "com.byread.dynamic";       // usb device connection        private static final String SYSTEMACTION =  intent.action_power_connected;        @Override         public voId oncreate (bundle savedinstancestate)  {            super.oncreate (savedinstancestate);            Setcontentview (r.layout.main);           sendstaticbtn =   (Button)  findviewbyid (r.id.send_static);            sendDynamicBtn =  (Button)  findviewbyid (r.id.send_dynamic);            sendSystemBtn =  (Button)  findviewbyid (R.id.send_system);            sendstaticbtn.setonclicklistener (new  Myonclicklistener ());            Senddynamicbtn.setonclicklistener (New myonclicklistener ());            sendsystembtn.setonclicklistener (new myonclicKlistener ());       }       class  myonclicklistener implements onclicklistener{             @Override            public void onclick (View  V)  {               //  Send custom static registered broadcast messages                if (V.getid ()  == r.id.send_static) {                    LOG.E ("mainactivity",  "Send custom static registration Broadcast message");                    intent intent = new  intent ();                    intent.setaCtion (staticaction);                    intent.putextra ("MSG", ) received a static registration broadcast successfully. ");                    sendbroadcast (Intent);                }               //  Send custom dynamic registration broadcast messages                else if ( V.getid ()  == r.id.send_dynamic) {                    LOG.E ("mainactivity",  "Send custom Dynamic registration Broadcast message");                    intent intent =  new intent ();                    intent.setaction (dynamicaction);                    intent.putextra ("msg ", " received the dynamic registration broadcast successfully. ");  

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.