简体   繁体   中英

Android - Java - WebView >> Count Page Loading time, if not loaded, intent new activity

WebView web;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Bundle extras = getIntent().getExtras();  
    String uid = extras.getString("uid"); 
    web = (WebView) findViewById(R.id.webview01);
    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://xxx.xxx.xxx.xxx/android/login.php?uid="+uid);
                                                }

public class myWebClient extends WebViewClient  {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
                                                                        }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)   {
        view.loadUrl(url);
        return true;
                                                                        }
                                                }

This is the code of my webview activity. I'd like to implement a page loading function which is It will count in background when URL called.If URL doesnt load completely in 5000ms, the code below must run automatically. I cant figure out how to figure It.

            Intent i = new Intent(MainActivity.this, InternetError.class);
            startActivity(i);

I've tried lot of methods but never get correct result. Thank you.

First, please change you implementation of shouldOverrideUrlLoading so that it does not call loadUrl on the WebView, it's wrong to do that. The URL load is in progress when you get that callback, you must not start it again.

Second, to solve your issue can you post a delayed message to a Handler that will execute your error code after the desired timeout, and remove that message if WebViewClient.onPageFinished comes first?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM