如果网页未加载则发送提醒

时间:2011-09-19 19:22:37

标签: android web-services alertdialog

我正在构建一个链接到多个网页的Android应用,但是如果网页没有加载告诉用户没有连接,我想发送提醒。如何检查网页是否加载?

谢谢!

1 个答案:

答案 0 :(得分:1)

我相信这段代码可以帮到你!

onReceivedError(),onProgressChanged()可以和onPageFinished()一起使用,以帮助您与用户交互!

public class WebViewTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Let's display the progress in the activity title bar, like the
        // browser app does.
        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        WebView webview = new WebView(this);
        setContentView(webview);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);

        webview.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
          public void onProgressChanged(WebView view, int progress) {
            // Activities and WebViews measure progress with different scales.
            // The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 1000);
          }
        });

        webview.setWebViewClient(new WebViewClient() {
        ProgressDialog MyDialog;

          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
          }

        });

        webview.loadUrl("http://cnn.com/";);

    }
}