public static boolean getNetWorkInfo(Context cont) {
try {
ConnectivityManager manager = (ConnectivityManager) cont
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return false;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()
|| !networkinfo.isConnectedOrConnecting()) {
return false;
} else {// /如果有网络连接,再判断一下是否可以正常上网,
if (openUrl()) {// //正常
return true;
} else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean openUrl() {
String myString = "";
try {
URL url = new URL("HTTP://www.baidu.com/index.html");
URLConnection urlCon = url.openConnection();
urlCon.setConnectTimeout(1500);
InputStream is = urlCon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
// 用ByteArrayBuffer缓存
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
myString = EncodingUtils.getString(baf.toByteArray(), "UTF-8");
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
if (myString.indexOf("www.baidu.com") > -1) {
return true;
} else {
return false;
}
}
public boolean isWifiConnected(Context context) {
if (context != null) {
return getNetWorkInfo(context);
}
return false;
}
Android 判断网络是否可以访问Internet
最新推荐文章于 2021-05-25 19:04:25 发布