Broadcast receiver Activity
If you want to catch a broadcasted intent on an Activity, you may get the following error:
- 02-22 08:18:46.874: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to instantiate receiver com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver:
- ...
- java.lang.InstantiationException: com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver
- ...
- 02-22 08:18:46.874: E/AndroidRuntime(276): Caused by: java.lang.InstantiationException: com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver
This is because you can't instantiate a receiver in an inner class.
Instead of inner receiver, you can manually instantiate a broadcast receiver yourself in the activity.
No need to set this receiver in the manifest xml file, register it in the activity's onresume method and unregister in the onpause:
- public void onResume() {
- super.onResume();
- ....
- registerReceiver(myBroadcastReceiver, new IntentFilter("your.custom.BROADCAST&qu
ot;)); - }
- public void onPause() {
- super.onPause();
- ...
- unregisterReceiver(myBroadcastReceiver);
- }
- ...
- }
Thats all, the receiver will catch the broadcasts, if the activity is on the screen.
To broadcast custom intents, use the following method:
- Intent broadCastIntent = new Intent();
- broadCastIntent.setAction( "your.custom.BROADCAST&qu
ot; ); - broadCastIntent.setPackage( "com.helloandroid.broadca
sttest" ); - ApplicationObject.applicationContext.sendBroadcast( broadCastIntent );
- Log.d( "Broadcast sent" );
The setPackage() method set an explicit application package name that limits the components the Intent will resolve to.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
12 years 4 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
12 years 4 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
12 years 4 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
12 years 4 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
12 years 4 weeks ago
Poll
Useful resources
Android Development Projects
- App Development
- Telegram bulk messages sender
- Photo Editing Application
- React Native Developer - part time basis
- Locket App Copy
- Courier app $150 only
- Mobile Application Development - IOS and Android (Rewards for sharing brand ads on social media)
- Android app for sms
- Fitness app project for sale online
- io developer