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; } }
请求当前网络状态
最新推荐文章于 2024-04-18 14:05:36 发布