通过自定义webview实现类似IOS webview中的stringByEvaluatingJavaScriptFromString方法获取js中的文本简介,代码如下:
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public String stringByEvaluatingJavaScriptFromString(String script) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
try {
Field mp = WebView.class.getDeclaredField("mProvider");
mp.setAccessible(true);
Object webViewObject = mp.get(this);
Field wc = webViewObject.getClass().getDeclaredField("mWebViewCore");
wc.setAccessible(true);
Object webViewCore = wc.get(webViewObject);
Field bf = webViewCore.getClass().getDeclaredField("mBrowserFrame");
bf.setAccessible(true);
Object browserFrame = bf.get(webViewCore);
Method stringByEvaluatingJavaScriptFromString = browserFrame.getClass()
.getDeclaredMethod("stringByEvaluatingJavaScriptFromString",