Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Intents and Broadcast Receivers Broadcast Receivers Registering in the Manifest

Robin Damsgaard Larsen
Robin Damsgaard Larsen
13,929 Points

Declaring BroadcastReceivers in the manifest is deprecated

As per the provided link in the teacher's notes, https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges declaring BroadcastReceivers in the manifest is deprecated from Android 7.0 and up. From what I gather, this is in an effort to lower battery consumption.

What I did to receive the broadcast instead was to register the broadcast receiver in MainActivity's onCreate method:


IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(new NetworkConnectionReceiver(), intentFilter);

(see also: https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)


Registering a Broadcast Receiver in code is what the next video after the coding challenge is about, and it is shown in which lifecycle methods it's best to un/register Broadcast Receivers (unlike what I did in my code above).

Zack Osborn
Zack Osborn
8,524 Points

Thanks for the info! Saved me a headache for sure.

Thank you!!

1 Answer

Tonnie Fanadez
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,795 Points

I noted some manifest-declared receivers work just fine when declared on the manifest e.g. android.intent.action.AIRPLANE_MODE.

However, these receivers have to be registered dynamically inside the code for them to work: *android.intent.action.SCREEN_ON *android.intent.action.SCREEN_OFF *android.intent.action.HEADSET_PLUG *android.intent.action.TIME_TICK *android.net.conn.CONNECTIVITY_CHANGE (ConnectivityManager.CONNECTIVITY_ACTION) *android.intent.action.CONFIGURATION_CHANGED