"Android Notes" as the broadcast recipient of an internal class

Source: Internet
Author: User

In the article "Comprehensive Parsing service components," we mentioned the use of broadcast receivers (BROADCASTRECEIVER) to communicate service to activity. In this example, we define a custom broadcast receiver in the service subclass that listens to the specific broadcasts emitted by the activity and thus triggers the OnReceive method, in which we indirectly invoke the service class method. Thus, the communication between service and activity is realized. The broadcast receivers in the example are registered dynamically (that is, using the Registerreceiver and Unregisterreceiver methods), but we know that the broadcast receiver also has a static way of registering, which is configured in the manifest file Receiver node (the reason why you are not using static registration is because there are some areas to be aware of).

So here's a static way to register a broadcast receiver as an internal class .

1.activity definition (layout is simple, just a button, no code)

Package Com.example.brocastdemo;Import android.app.Activity;Import Android.content.BroadcastReceiver;Import Android.content.Context;Import android.content.Intent;Import Android.os.Bundle;Import Android.util.Log;Import Android.view.View;Import Android.view.View.OnClickListener;Import Android.widget.Button;PublicClassMainactivityExtendsactivity{Private Button but =Null@OverrideProtectedvoid OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); but = (Button) Findviewbyid (r.id.but); But.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) {Intent Intent = new Intent ("Com.aa A "); Mainactivity. This.sendbroadcast (intent);} }); the public static class Myreceiver extends broadcastreceiver//as the broadcast recipient of the inner class { @Override public void OnReceive (context context, Intent Intent) { if (intent.getaction (). Equals ("com.aaa")) { LOG.I ("mainactivity","successfully received broadcast");            }}} 
2. manifest file:
<receiver             android:name="Com.example.brocastdemo.mainactivity$myreceiver"            >             <<android:name=</</receiver>       

After the code is written, we test it and click button to find that Logcat successfully printed the log and tested successfully.


The following summarizes the broadcast receivers as internal classes when registering places to be aware of :

1. The name format of the broadcast recipient should be noted when registering the broadcast receiver with the manifest file. because it is an inner class, you need to add a $ symbol between the class that contains the inner class and the inner class:

Android:name="com.example.brocastdemo.MainActivity$MyReceiver" 

2. the inner class must be written as a static inner class (plus static before the class keyword) when declaring. Otherwise, an exception would be thrown like this:

02-19 09:11:14.650:e/androidruntime (1771): Java.lang.RuntimeException:Unable to instantiate receiver Com.example.brocastdemo.MainActivity$MyReceiver: Java.lang.InstantiationException:can' t instantiate class Com.example.brocastdemo.mainactivity$myreceiver; No empty constructor 

You may find it inconvenient to use statically registered broadcast receivers as internal classes because they are static internal classes, so if you want to use the variables/methods of an external class, the variable/method is static.

So it is strongly recommended that you use dynamic mode of registration, the following example to change the dynamic mode of registration.


Modified activity:

Package Com.example.brocastdemo;Import android.app.Activity;Import Android.content.BroadcastReceiver;Import Android.content.Context;Import android.content.Intent;Import Android.content.IntentFilter;Import Android.os.Bundle;Import Android.util.Log;Import Android.view.View;Import Android.view.View.OnClickListener;Import Android.widget.Button;Dynamically register a broadcast receiver as an internal classPublicClassMainactivityExtendsactivity{Private Button but =NullPrivate Myreceiver receiver =Null@OverrideProtectedvoid OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); but = (Button) Findviewbyid (r.id.but);/* Dynamically register the broadcast receiver */receiver =New Myreceiver (); Intentfilter filter =New Intentfilter (); Filter.addaction ("Com.aaa");This.registerreceiver (receiver, filter); But.setonclicklistener (New Onclicklistener () {@OverridePublicvoid OnClick (View v) {Intent Intent =New Intent ( "com.aaa"); Mainactivity. this.sendbroadcast (intent);} }); } public class myreceiver span class= "keyword" >extends broadcastreceiver { @Override public void onreceive (context context, Intent Intent) {if ( Intent.getaction (). Equals ( "COM.AAA")) {log.i ( "mainactivity",  "successfully received broadcast"); }}}  @Override protected void OnDestroy () { span class= "keyword" >if (receiver!=null) this.unregisterreceiver ( Receiver); }} 

At this point, the receiver node is not used in the manifest file. Test of course is also passed, so it is more convenient!

"Android Notes" as the broadcast recipient of an 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.