2
你好,我是新的android,我实现了一个方法来检查互联网连接。它在一些设备工作正常,不工作在三星galaxy选项卡4.我搜索了很多,我找到一个解决方案。我的设备可以连接其他应用程序的互联网。它在所有其他设备上运行良好。请帮我在此先感谢Android网络检查不在某些设备上工作
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;
}