Android :网络判断工具类

package com.example.wifinetworkdemo.util;

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

public class NetworkUtils {
    //为什么要串上下文对象  因为这是一个单独java类所以获取不到Android
    public static boolean getNetwork(Context context) {
        boolean boo = false;
        ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }

}

以上

猜你喜欢

转载自blog.csdn.net/weixin_43603192/article/details/84222905