What is the solution to Check internet connectivity [duplicate]

徘徊边缘 提交于 2020-01-14 07:09:26

问题


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


回答1:


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




回答2:


    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
    }



回答3:


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
        }
    }
};



回答4:


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
}


来源:https://stackoverflow.com/questions/39408697/what-is-the-solution-to-check-internet-connectivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!