美文网首页
判断是否有网

判断是否有网

作者: 努力努力再努力_姜姜 | 来源:发表于2016-05-29 12:14 被阅读37次

    NetTestUtil

    public class NetTestUtil {
        public static boolean netTest(Context context){
            ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager==null){
                Toast.makeText(context,"断网了,请连接网络后重试!",Toast.LENGTH_SHORT).show();
                return false;
            }
            NetworkInfo netInfo=connectivityManager.getActiveNetworkInfo();
            if(netInfo==null){
                Toast.makeText(context,"断网了,请连接网络后重试!",Toast.LENGTH_SHORT).show();
                return false;
            }
            if(netInfo.isConnected()){
                return  true;
            }
            return false;
        }
    }
    

    相关文章

      网友评论

          本文标题:判断是否有网

          本文链接:https://www.haomeiwen.com/subject/islqdttx.html