网络判断

//判断是否有网络
public static int getNetPyte(Context context){
    //变量
    int mState=-1;//-1没有网络
    //获取ConnectivityManager
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   //获取网络连接的信息
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    //判断是否有网络
    if (activeNetworkInfo == null) {
        return mState;
    }
    
    //获取网络状态
    int type = activeNetworkInfo.getType();
    //判断是否是蜂窝网络
    if (type == ConnectivityManager.TYPE_MOBILE) {
        mState=0;
    }else if (type == ConnectivityManager.TYPE_WIFI) {//判断是否是wifi网络
        mState=1;
    }

    return mState;
}
阅读更多

没有更多推荐了,返回首页