how to distinguish url form user clicking or autom

2019-06-04 06:37发布

问题:

In my webview , I get all url in the webViewClient method

public boolean shouldOverrideUrlLoading(WebView view, String url) {}

I want to throw the url to other application if the url is from user clicking. Otherwise webview load the url self.But I can't distinguish them.How should I do?

Thanks for your help!

回答1:

I follow the method in How can I get onclick event on webview in android? .

there are some alternatives using dispatchTouchEvent Android singleTap/OnClick in WebView

Boolean changedUrl = false;
String currentUrl = null;

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        changedUrl = false;
        currentUrl = webView.getUrl();
    }

   // change either onPageStarted or shouldOverrideUrlLoading
     @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if(changedUrl && !url.equals(currenturl)) {
              // page has been clicked
        }

    }

    @Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
    if(changedUrl && !url.equals(currentUrl)) {
          // page has been clicked

          return true;
    }

    return false;
}


 });

webView.setOnTouchListener( new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.webView && event.getAction() == MotionEvent.ACTION_DOWN){

              changedUrl = true;

        }

        return false;
    }
});