onReceivedTitle网址更改时,下面的方法webview每秒调用2-3次。我想在上次调用onReceivedTitle时在其中调用方法。我这样做是因为我只想在Webview中监视url的更改。当URL通过Ajax更改时,不会调用shouldOverrideUrlLoading

class MyWebChromeClient extends WebChromeClient {


        @Override
        public void onReceivedTitle(WebView view, String title) {
            Log.v("onReceivedTitle", "=>" + title);

            // callAMehod();
            super.onReceivedTitle(view, title);
        }

    }

最佳答案

您可能要使用Handler并执行以下操作:

class MyWebChromeClient extends WebChromeClient {

    private boolean mOnReceivedTitleInvoked;

    @Override
    public synchronized void onReceivedTitle(final WebView view, final String title) {
        if (!mOnReceivedTitleInvoked) {
            mOnReceivedTitleInvoked = true;
            Log.v("onReceivedTitle", "=>" + title);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    super.onReceivedTitle(view, title);
                    mOnReceivedTitleInvoked = false;
                }
            }, 1000);
        }
    }

}


尽管您可能想重新考虑onReceivedTitle行为。

10-01 05:26