Android public static internal class broadcastreceiver

Source: Internet
Author: User

Android public static internal class broadcastreceiver

The first time I wrote a blog, I hope you can correct it.

When you want to implement the broadcast receiving function, Because Java is a single inheritance, generally one class will be extends activity, so you will think of the internal class extends broadcastreceiver, which may encounter two exceptions.

1: Java. Lang. runtimeexception: Unable to instantiate
Receiver com. example. progressbar. progressbaractivity $ mbroadcastreceiver:
Java. Lang. instantiationexception: COM. example. progressbar. progressbaractivity $ mbroadcastreceiver

This is because the internal class needs to be written as static, that is, static

StaticClass mbroadcastreceiver extends broadcastreceiver {
// Public static)

@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
}
}

2: Java. Lang. runtimeexception: Unable to instantiate
Receiver com. example. progressbar. progressbaractivity $ mbroadcastreceiver:
Java. Lang. illegalaccessexception: Access to class not allowed

This is because you need to write this class as public. If the default value is protected, access to class not allowed will appear.

Correct:

Public staticClass mbroadcastreceiver extends broadcastreceiver {
// Public static)
@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
}
}

Add $ to the internal class

<Cycler Android: Name = "com. example. progressbar. progressbaractivity$Mbroadcastreceiver ">
<Intent-filter>
<Action Android: Name = "com. example. progressbar. progressbaractivity. mbroadcastreceiver"/>
</Intent-filter>
</Cycler>

Of course, there is another way to implement its instance.

Private broadcastreceiver mbroadcastreceiver = new broadcastreceiver (){
@ Override
Public void onreceive (context, intent ){


}
};

Then register and log out.

Call registerboradcastreceiver () in oncreate () or onresume ()

Public void registerboradcastreceiver (){
Intentfilter myintentfilter = new intentfilter ();
Myintentfilter. addaction (action_name );
// Register Broadcast
Registerreceiver (mbroadcastreceiver, myintentfilter );
}

@ Override
Public void ondestroy (){
Super. ondestroy ();
Unregisterreceiver (mbroadcastreceiver );
}

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.