6

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.

Arsen Nersisyan
  • 458
  • 5
  • 18
  • 1
    https://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 Answers1

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