Kitkat doesn't return url for requestFocusNodeHref

安稳与你 提交于 2019-12-23 02:56:08

问题


I have a long click listener attached to a webview, inside it I obtain the url associated to an img tag, everything works fine on 4.2.2 and 4.3 but now on 4.4 the message data url is identical to src's value

requestFocusNodeHref documentation

Is this a bug?

public class ImagePickerActivity extends Activity implements OnLongClickListener {
  ...
  ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    webView = (WebView) findViewById(R.id.webview_view);
    webView.setWebViewClient(...);
    webView.setWebChromeClient(...);
    webView.setOnLongClickListener(this);
}

@Override
public boolean onLongClick(View v) {
    Message msg = new Message();
    msg.setTarget(new LongClickHandler(this));
    webView.requestFocusNodeHref(msg);
    return false;
}


private final static class LongClickHandler extends Handler {
    private final ImagePickerActivity activity;

    public LongClickHandler(ImagePickerActivity activity) {
        this.activity = activity;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String url = msg.getData().getString("url");

        // as documentation says, three keys are returned: url, src and title
        // but on kitkat 4.4 url and src are identical
        // on 4.2.2 and 4.3 as written on docs url points to the anchor's href attribute and src to the img.src attribute 
        for (String s : msg.getData().keySet()) {
            System.out
                    .println("ImagePickerActivity.LongClickHandler.handleMessage()" + s + " = " + msg.getData().get(s));
        }
    }
}

}


回答1:


This is most likely a bug. I filed https://code.google.com/p/chromium/issues/detail?id=323989 to track this, but even if a fix for this lands it might not make its way to devices for a while.

Unfortunately there isn't an easy way to work around this (sorry!), some suggestions (from simplest to most complicated):

  • encode the href in the image src URL (requires you to modify the content and that your server ignores some bit of the image src URL, like the query bit),
  • if the images have unique URLs use WebView.evaluateJavascript to find the image and get the parent's href,
  • use JS to detect that the element has been focused and hand the href back to Java via an object exposed to JS via addJavascriptInterface,
  • use something like JQuery Mobile taphold to handle the long-press in JS,
  • use document.elementFromPoint. To calculate the CSS coordinates you may need to use View.getLocationOnScreen (to go from absolute to WebView-relative coordinates), WebView.getScrollX/Y (to add the WebView scroll offset to the view-relative touch point coordinates) and finally divide by WebView.getScale() to get the CSS pixel values. After that you could use WebView.evaluateJavascript to get the result.


来源:https://stackoverflow.com/questions/20244661/kitkat-doesnt-return-url-for-requestfocusnodehref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!