Is there any way to handle wi-fi or cellular networks enable/disable on android N. I've tried to add broadcast receiver with intent-filter "android.net.conn.CONNECTIVITY_CHANGE"
, but it's deprecated for N and higher.
Asked
Active
Viewed 5,523 times
6

Arsen Nersisyan
- 458
- 5
- 18
-
1https://developer.android.com/topic/performance/background-optimization.html#connectivity-action – CommonsWare Oct 26 '16 at 15:19
-
Thanks, I've read it many times, but I've not seen this `Apps that are running can still listen for CONNECTIVITY_CHANGE on their main thread by registering a BroadcastReceiver with Context.registerReceiver().` – Arsen Nersisyan Oct 26 '16 at 15:50
-
The idea there is that if your app is already running for other reasons, you can keep track of connectivity changes via broadcasts. – CommonsWare Oct 26 '16 at 15:54
1 Answers
16
The network changes listener implementation for android N and high
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
registerReceiver(mNetworkReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
Many thanks to CommonsWare for useful answer.

Derlin
- 9,572
- 2
- 32
- 53

Arsen Nersisyan
- 458
- 5
- 18
-
3But it will be killed once the activity which registers this receiver is killed? – Paras Watts Sep 14 '17 at 16:00
-
-
I am doing this now using the firebase job dispatcher. Working fine. – Paras Watts Feb 09 '18 at 03:41