0
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(){});


}
}

如何隐藏或替换网页不可用页面?(请让我知道在哪里放置代码以隐藏或替换它)

谢谢

4

1 回答 1

1

您可以在加载到 WebView 之前检查是否有任何连接(Wi-Fi 或 3G)。

创建一个Utility类(说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();

}
}

希望这会有所帮助。

于 2013-10-24T00:57:52.277 回答