像 Iphone 这样的 Android 中是否存在 stringByEvaluatingJavaScriptFromString 方法?不是像这样的简单代码 javascript:wave()。但具有复杂的 Java Script 功能。谢谢
问问题
3608 次
2 回答
0
stringByEvaluatingJavaScriptFromString 是 Android API 中的私有方法。但它真的很有用。
您可以通过 Java 反射检索此 API:
Method stringByEvaluatingJavaScriptFromString , sendMessageMethod;;
Object webViewCore , browserFrame;
private boolean hasIntercepted = true;
Object webViewObject = this;
Class webViewClass = WebView.class;
try {
Field mp = webViewClass.getDeclaredField("mProvider");
mp.setAccessible(true);
webViewObject = mp.get(this);
webViewClass = webViewObject.getClass();
Field wc = webViewClass.getDeclaredField("mWebViewCore");
wc.setAccessible(true);
webViewCore = wc.get(webViewObject);
if (webViewCore != null) {
sendMessageMethod = webViewCore.getClass().getDeclaredMethod("sendMessage", Message.class);
sendMessageMethod.setAccessible(true);
Field bf= webViewCore.getClass().getDeclaredField("mBrowserFrame");
bf.setAccessible(true);
browserFrame = bf.get(webViewCore);
stringByEvaluatingJavaScriptFromString = browserFrame.getClass().getDeclaredMethod("stringByEvaluatingJavaScriptFromString", String.class);
stringByEvaluatingJavaScriptFromString.setAccessible(true);
}
hasIntercepted = true;
} catch (Throwable e) {
hasIntercepted = false;
}
然后使用这个方法:
Object argument =(Object) ((String)"window.hitTest("+event.getX()+ ","+event.getY()+")");
try {
Object _tagName = stringByEvaluatingJavaScriptFromString.invoke(browserFrame, argument);
String tagName =(String)_tagName;
} catch (Exception e) {
e.printStackTrace();
}
于 2013-07-24T09:43:54.227 回答
0
您可以loadUrl
改用,例如:
webView.loadUrl("javascript:PerformSimpleCalculation()");
效果类似stringByEvaluatingJavaScriptFromString
。API描述不清楚。
于 2012-01-06T09:55:03.477 回答