Android Radio (1)

Source: Internet
Author: User

(The following is a summary of the reading of the first line of Guo Lin, the Great God)

1. Overview

Broadcast is one of the four components of Android.

Android's broadcast mechanism is very flexible.

2. Send Broadcast

such as Android broadcasts are divided into two main categories: Standard broadcast and ordered broadcast.

All broadcast receivers can receive standard broadcasts, and their reception is almost at the same time. This means that standard broadcasts are highly efficient, but cannot be truncated.

When receiving an orderly broadcast, the broadcast receiver is ordered, the priority receiver receives the broadcast first, and has the right to truncate the broadcast and not allow subsequent receivers to receive it.

The way to send standard broadcasts and ordered broadcasts is simple.

 //  send standard broadcast  Sendbroadcast.setonclicklistener (new   View.onclicklistener () {@Override  public  void   OnClick (View v) {Intent Intent  = new  Intent (" Com.example.broadcastTest.MY_BROADCAST "
// send ordered broadcast Sendbroadcast.setonclicklistener (new  View.onclicklistener () {                @Override     publicvoid  OnClick (View v) {        new Intent (" Com.example.broadcastTest.MY_BROADCAST ");        Sendorderedbroadcast (Intent,null);    }});

are annotations to Sendbroadcast () and Sendorderedbroadcast () in the Android API.

3. Receiving broadcasts

Of course, there is no receiver after sending the broadcast.

3.1 Creating a Receiver

Creating a broadcast receiver is simple, just create a new class, let it inherit broadcastreceiver, and rewrite the OnReceive () method in the parent class.

// Create a broadcast receiver  Public class extends broadcastreceiver{        @Override    publicvoid  onreceive (Context context,intent Intent) {        Toast.maketext (context,"Network change!") ,            Toast.length_short). Show ();    }    }

3.2 Registering a broadcast receiver

Of course, it is useless to create only broadcast receivers. The broadcast receiver also needs to register to indicate what kind of broadcast it can receive.

Receiver registration is divided into dynamic registration and static registration.

Dynamic registration is done by registering in the code. Static registration is registered in the Androidmanifest.xml.

3.2.1 Dynamic Registration

Dynamically registered broadcasts require a program to start to receive broadcasts.

@Override protected void onCreate (Bundle savedinstancestate) {    super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main);     New Intentfilter ();    //         Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE"); //    New Networkchangereceiver (); //    Registerreceiver (Networkchangereceiver,intentfilter); //}   

Register the receiver dynamically, be sure to unregister it.

@Override protected void OnDestroy () {    super. OnDestroy ();    Unregisterreceiver (Networkchangereceiver);}  

The following is a description of Registerreceiver () and Unregisterreceiver () in the Android API.

3.2.2 Static Registration

Static registration does not require the program to start, the phone can be opened to receive the broadcast. The static registration is stated in the Androidmanifest.xml.

<android:name= ". Networkchangereceiver "><intent-filter><  Android:name = "Android.net.conn.CONNECTIVITY_ change" /> </ Intent-filter > </ receiver >        

4 Summary

In summary, using the Android broadcast mechanism, we need to do the following points

If you listen to a custom broadcast,

1. Use the Sendbroadcast () or Sendorderedbroadcast () method to send a defined broadcast

2. Create a broadcast receiver, inherit from Broadcastreceiver (), and override the OnReceive () method. The response action received after the broadcast is implemented in the OnReceive () method.

3, register the broadcast receiver, so that the broadcast receiver can receive the broadcast. Broadcast receivers can be dynamically registered or statically registered. Registration is required for dynamic registration.

If it is a listening system broadcast, the above three steps we only need to do the next two steps will be OK.

Android Radio (1)

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.