Android broadcast (internal class) Usage

Source: Internet
Author: User

1. Broadcast is defined in a separate file

Source code:

public class MessageReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubif(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>

Use the code to register:

// Generate messagereceiver smsbroadcastreceiver = new messagereceiver (); // instantiate the filter and set the broadcast intentfilter = new intentfilter (); intentfilter. addaction ("android. provider. telephony. sms_received "); // register broadcast this. registerreceiver (smsbroadcastreceiver, intentfilter );

Cancel broadcast:

unregisterReceiver(smsBroadCastReceiver);

2. Broadcast is an internal class

public class SettingForSMS extends PreferenceActivity{@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.layout.sms_setting);}public static class MessageChangeReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        Log.i(tag, "onReceive");    }}}

Note that it must be declared as static during the definition process, otherwise 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 that the internal class uses the $ symbol instead of the. symbol. If you use the. symbol, the class cannot be found.

Register in the program. The register is placed in oncreate, And the unregister is placed in ondestroy.

Smsbroadcastreceiver = new messagechangereceiver (); // instantiate the filter and set the broadcast intentfilter = new intentfilter (); intentfilter. addaction ("com. ginwave. message_changed "); // register broadcast this. registerreceiver (smsbroadcastreceiver, intentfilter );
unregisterReceiver(smsBroadCastReceiver);

I hope it will be easier to use it later ~~

 

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.