判断是否有网

2016-05-29  本文已影响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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读