import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl(" my site ");
webView.setWebViewClient(new WebViewClient(){});
}
}
如何隐藏或替换网页不可用的页面?(请告知我将代码隐藏或替换的位置)
由于
答案 0 :(得分:1)
您可以在加载到WebView之前检查是否有任何连接(Wi-Fi或3G)。
创建一个Utility类(Say AppUtility.java)编写此方法
/**
* Determine connectivity. a utility method to determine internet
* connectivity this is invoked before every web request
*
* @param ctx
* the ctx
* @return true, if successful
*/
public static boolean determineConnectivity(Context ctx) {
ConnectivityManager manager = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info != null && info.getState() == NetworkInfo.State.CONNECTED;
}
所以现在在WebView中加载页面之前检查连接性
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){});
if(AppUtility.determineConnectivity())
webView.loadUrl(" my site ");
else
Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
希望这会有所帮助。