I am creating a BroadcastReceiver to listen for Wi-Fi connectivity changes:
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); // deprecated
// Get connectivity details from networkInfo...
}
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
However, ConnectivityManager.EXTRA_NETWORK_INFO
is deprecated, so how else to get the NetworkInfo
object?
You can use getActiveNetworkInfo();
ConnectivityManager connectivityManager = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With