奇巧不会为requestFocusNodeHref返回URL [英] Kitkat doesn't return url for requestFocusNodeHref
问题描述
我必须连接到网页视图长时间点击监听器,它里面的我获得与一个 IMG
标记的URL,一切工作正常4.2.2和4.3,但现在4.4对信息数据网址
与的src
的价值
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
<一个href=\"http://developer.android.com/reference/android/webkit/WebView.html#requestFocusNodeHref%28android.os.Message%29\"相对=nofollow> requestFocusNodeHref 文档
这是一个错误吗?
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));
}
}
}
}
推荐答案
这是最有可能的一个bug。我申请的https://$c$c.google.com / p /铬/问题/细节?ID = 323989 跟踪这一点,但即使这样的修复土地可能不是做它的方式给设备一段时间。
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):
- 连接code在图片src网址中的href(要求您修改的内容和你的服务器忽略了图片src网址的某些位,比如查询位),
- 如果图像具有独特的URL使用
WebView.evaluateJavascript
找到图像,并获得家长的href, - 使用JS来检测该元素一直专注和手在href回Java威盛addJavascriptInterface暴露给JS对象,
- 使用类似jQuery Mobile的 taphold 办理长期preSS的JS,
- 使用
document.elementFromPoint
。要计算CSS坐标,你可能需要使用View.getLocationOnScreen
(从绝对到的WebView相对坐标),WebView.getScrollX / Y(添加的WebView滚动偏移量该视图相对触摸点坐标),最后由WebView.getScale分为()
来得到CSS像素值。之后,你可以使用WebView.evaluateJavascript
来得到的结果。
- 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 useView.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 byWebView.getScale()
to get the CSS pixel values. After that you could useWebView.evaluateJavascript
to get the result.
这篇关于奇巧不会为requestFocusNodeHref返回URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!