判断网络

1.判断网络
public static boolean 方法名(Context context){

	//定一个变量
    boolean b=false;
    ConnectivityManager m= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = m.getActiveNetworkInfo();
    if(network!=null){
    	//不等于空 有网
        b= network.isAvailable();
    }
    return b;
}

2.判断是wife还是手机网络
方法

public static int getNetType(Context context) {

     int mState = -1;// 代表无网络
     
    //网络连接管理类
    ConnectivityManager m= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //nn是 网络状态类;
    NetworkInfo nn= m.getActiveNetworkInfo();

    if (nn== null) {
        return  mState;
    }

    //获取网络类型
    int type = networkInfo.getType();

    if (type == ConnectivityManager.TYPE_WIFI) { //1,wifi网络
        mState = 1;
        return mState;

    }else if(type == ConnectivityManager.TYPE_MOBILE){ //0,手机网络
        mState = 0;
        return mState;

    }

    return mState;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值