开发者

send an alert if a webpage does not load

开发者 https://www.devze.com 2023-04-06 03:36 出处:网络
I am building an Android app that links to several webpages, but I want to send an alert if the webpage doesn\'t load telling the user there is no connection. How do I check to see if the webpage load

I am building an Android app that links to several webpages, but I want to send an alert if the webpage doesn't load telling the user there is no connection. How do I check to see if the webpage loads?

Thanks开发者_开发问答!


I am sure this code will help you!

onReceivedError(), onProgressChanged() can be used as well as onPageFinished() in order to help you interacting with the user!

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/";);

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消