Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ConnectivityManager.EXTRA_NETWORK_INFO deprecated

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?

like image 777
ban-geoengineering Avatar asked Feb 06 '23 01:02

ban-geoengineering


1 Answers

You can use getActiveNetworkInfo();

ConnectivityManager connectivityManager = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
like image 144
Ganesh AB Avatar answered Feb 15 '23 06:02

Ganesh AB