[英]Android network checking is not working in some devices
Hello I am new in android ,I implemented a method to check internet connection.It works fine in some devices and not working in samsung galaxy tab 4. I searched a lot and didin't find a solution.My device can connect internet from other apps.It works fine in all other devices. 您好,我是android的新手,我实现了一种检查Internet连接的方法。它在某些设备上可以正常工作,但在三星银河标签4中无法正常工作。应用程序。它在所有其他设备上都能正常运行。 Please help me thanks in advance 请事先帮助我
public boolean isConnected() {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
// Network is available but check if we can get access from the
// network.
URL url = new URL("http://www.google.co.in");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(2000); // Timeout 2 seconds.
urlc.connect();
if (urlc.getResponseCode() == 200) // Successful response.
{
Log.d("response", "" + urlc.getResponseCode());
return true;
} else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Try: 尝试:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnected();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.