请求当前网络状态

package com.lbs.zuo_yue.Utils;

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

/**
 * author:Created by WangZhiQiang on 2017/11/17.
 */

public class Net_cf4 {
    /**
     * 返回值 -1:没有网络  1:WIFI网络   2:net网络
     */
    public static int getVettype(Context context) {
        int nettype = -1;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo == null) {
            return nettype;
        }
        //获取状态码
        int ntype = networkInfo.getType();
        if (ntype == ConnectivityManager.TYPE_MOBILE) {
            nettype = 2;
        } else if (ntype == ConnectivityManager.TYPE_WIFI) {
            nettype = 1;
        }
        return nettype;
    }
}


















package com.lbs.zuo_yue.chilfrags;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lbs.zuo_yue.R;
import com.lbs.zuo_yue.Utils.Net_cf4;

/**
 * author:Created by WangZhiQiang on 2017/11/16.
 */

public class cf4 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.cf4_view, container, false);
        /**
         * 返回值 -1:没有网络  1:WIFI网络   2:net网络
         */
        int vettype = Net_cf4.getVettype(getActivity());
        if(vettype == -1){
            Toast.makeText(getActivity(),"-1:没有网络",Toast.LENGTH_SHORT).show();
        }else if(vettype == 1){
            Toast.makeText(getActivity()," 1:WIFI网络",Toast.LENGTH_SHORT).show();
        }else if(vettype == 2){
            Toast.makeText(getActivity()," 2:net网络",Toast.LENGTH_SHORT).show();
        }
        return view;
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值