网络判断

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

public class NetWorkUtils {
    //判断网络是否连接
    public static boolean isNetWorkAvailable(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null) {
            return true;
        }

        return false;
    }

}

//使用

boolean b = NetWorkUtils.isNetWorkAvailable(this);

if (b){
    Toast.makeText(this, "有网", Toast.LENGTH_SHORT).show();
}else {
    Toast.makeText(this, "没网", Toast.LENGTH_SHORT).show();
}

你可能感兴趣的:(网路判断)