检查互联网连接的解决方案是什么?

时间:2016-09-09 09:49:32

标签: android android-studio

getnetworkInfo '不推荐使用,解决方案是什么?我正在使用 compilesdkversion 24。

4 个答案:

答案 0 :(得分:0)

您可以使用getActiveNetworkInfo()代替getnetworkinfo,因为之前还有其他一些因素未被考虑过,例如网络状态可能会因使用getnetworkinfo而导致app变化,因此已弃用{ {3}}

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
}

注意:在访问网络状态

之前,也要进行无效检查以确认它不为空

答案 1 :(得分: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();
        }

}

使用此课程检查网络连接

if(NetworkUtils.isConnected(getContext())) {
     //Call your network related task                      
  } else {
     //Show a toast displaying no network connection
    }

答案 2 :(得分:0)

使用ConnectivityManager获取网络访问权限。您可以使用BroadcastReceiver不断更新您的网络状态。

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

从您的网络类接收更新,在主类中使用此更新状态。 在课堂上注册Receiver 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
        }
    }
};

答案 3 :(得分:0)

简单方法!

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

如何使用只需检查

if (!isConnectingToInternet(getContext())) {
   // here no internet connection
}