问题描述
在尝试并在stackoverflow上搜索以解决android上的Internet连接错误的方法后,我什么都没找到。
我尝试了您可以在底部看到的代码,但是它无法解决Internet连接错误。 mWebView.loadUrl("file:///android_asset/myerrorpage.html");
每次在浏览器中的网址不是http://192./loc/index.php
时都会执行。
当我在index.php上进行重定向时,将显示错误文件。
我该如何更改它,或者知道任何人的代码以检查Internet连接可用性,然后执行某些操作?
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
if (ipAddr.equals("")) {
return false;
mWebView.loadUrl("file:///android_asset/myerrorpage.html");
} else {
return true;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_localy);
mWebView = (WebView) findViewById(R.id.webview);
// Brower niceties -- pinch / zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
// Load google.com
mWebView.loadUrl("http://192./loc/index.php");
}
}
} catch (Exception e) {
return false;
}
}
1楼
您的问题不是很清楚。 如果需要检查连接,则需要:
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
然后,如果需要检查真实的互联网连接,则可以尝试以下操作:
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
if (ipAddr.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
并将ACCESS_NETWORK_STATE权限添加到清单。