-2

getnetworkInfo' is deprecated , what is the solution? I am using compilesdkversion 24.

Shashi
  • 29
  • 6

4 Answers4

1

Simple way !

  public static boolean isConnectingToInternet(@NonNull Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE || info.getType() == ConnectivityManager.TYPE_ETHERNET || info.getType() == ConnectivityManager.TYPE_WIMAX) {
                    return true;
                }
            }
        }
        return false;
    }

How to use just check

if (!isConnectingToInternet(getContext())) {
   // here no internet connection
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
0

you can use getActiveNetworkInfo() instead of getnetworkinfo , because there were some other factors which weren't considered before ,like network state can vary app to app while using getnetworkinfo hence deprecated docs

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if(activeNetwork!=null && activeNetwork.isConnectedOrConnecting()){
  // yeah we are online
}else{
  // oops! no network
}

Note : put a nullity check too to confirm it is not null before accessing the network status

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0
    public class NetworkUtils {

        public static boolean isConnected(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

            return (activeNetwork != null)
                    && activeNetwork.isConnectedOrConnecting();
        }

}

Use this class to check network connection as:

if(NetworkUtils.isConnected(getContext())) {
     //Call your network related task                      
  } else {
     //Show a toast displaying no network connection
    }
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

Use ConnectivityManager to get network access. you can use BroadcastReceiver to get constant updates of your network status.

package your_package_name;

import android.content.Context;
import android.content.ContextWrapper;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectivityStatus extends ContextWrapper{
public ConnectivityStatus(Context base) {
    super(base);
}

public static boolean isConnected(Context context){

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo connection = manager.getActiveNetworkInfo();
    if (connection != null && connection.isConnectedOrConnecting()){
        return true;
    }
    return false;
 }
}

Receive updates from your network class, use this in your main class where to update status. Register Receiver on class getContext().registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
        if(!ConnectivityStatus.isConnected(getContext())){
            //not connected
        }else {
            //connected
        }
    }
};