Android Broadcast (internal class)

Source: Internet
Author: User

1. Broadcasts are defined in a separate file

Source:

 Public class Messagereceiver extends broadcastreceiver{        @Override    publicvoid  onreceive ( Context context, Intent Intent) {        //  TODO auto-generated method stub        if( Intent.getaction (). Equals ("android.provider.Telephony.SMS_RECEIVED")) {                }    }}

Register in Manifest:

<receiver android:name=". Messagereceiver">            <intent-filter>                <action android:name="  Android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter></receiver>

Register with code:

// generate broadcast processing   Messagereceiver  New  messagereceiver ();   // instantiate the filter and set the broadcast  to be filtered New Intentfilter ();  Intentfilter.addaction ("android.provider.Telephony.SMS_RECEIVED");     // Register the  broadcast this. Registerreceiver (Smsbroadcastreceiver, intentfilter);   

To unregister a broadcast:

Unregisterreceiver (Smsbroadcastreceiver);

2. Broadcast is an internal class

 Public classsettingforsms extends preferenceactivity{@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Addpreferencesfromresource (r.layout.sms_setting); }         Public Static classMessagechangereceiver extends Broadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method StubLOG.I (Tag,"OnReceive"); }    }}

Note the definition must be declared as static, or it will not be found

Register in Manifest:

<receiver android:name=". Settingforsms$messagechangereceiver">            <intent-filter>                <action android:name= " com.ginwave.message_changed "/>            </intent-filter></receiver>

Note the inner class uses the $ symbol instead of., using the. Class could not find an exception

Register within the program, register is placed in the OnCreate, unregister is placed in the OnDestroy.

New messagechangereceiver ();           // instantiate the filter and set the broadcast          to be filtered New Intentfilter ();          Intentfilter.addaction ("com.ginwave.message_changed");                     // Register a  broadcast         This
Unregisterreceiver (Smsbroadcastreceiver);

Note: If you use the GetResource () method, use Context.getresource ()

Android Broadcast (internal class)

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.