Android: Subclass of BroadcastReceiver must be declared as public
/* BroadcastReceiver */
I wanted to send and receive my own broadcast intent, so I defined MyBroadcastReceiver class in MyAndroidActivity class which is the main activity class launched by Android. Since Java language doesn’t allow to define two or more public classes in same Java source file, I declared MyBroadcastReceiver class with no modifier (package-private). As a result, I got cryptic exception “IllegalAccessException”:
DalvikVM[localhost:8600]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.handleReceiver(ActivityThread$ReceiverData) line: 1773
ActivityThread.access$2400(ActivityThread, ActivityThread$ReceiverData) line: 117
ActivityThread$H.handleMessage(Message) line: 981
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
Thread [<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1] (Running)
After a bit struggling, I found out the answer: subclass of BroadcastReceiver must be declared with public modifier
A subclass of BroadcastReceiver is called from Android system, so I had to define it with public modifier. This means you can’t define it in same Java source file. I tried to define it as public inner class of MyAndroidActivity but failed.
Thanks to this page: [StackOverflow] Android: java.lang.IllegalAccessException when attempting to use a custom “Application” class
Thanks Admin Good Sites
Android
8 Nov 11 at 08:02