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 trial

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:

...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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)


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).

Zack Osborn
Zack Osborn
8,524 Points

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

Thank you!!

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 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