Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRobin Damsgaard Larsen
13,929 PointsDeclaring 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:
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(new NetworkConnectionReceiver(), intentFilter);
...
EDIT:
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).
Paritosh Sahni
Courses Plus Student 4,853 PointsThank you! Definitely saved me a lot of time.
patrick ridd
21,011 PointsThank you!!
1 Answer
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsI 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
Zack Osborn
8,524 PointsZack Osborn
8,524 PointsThanks for the info! Saved me a headache for sure.