Android webview中打印DomTree和RenderTree的方法
Android 4.4以前的在framework层提供了提供了打印网页DomTree和RenderTree的方法。但是应用中一般没办法调用到这个API,前段时间也研究了一下如何用反射的方法调用到这个API,不多说了,直接看Demo:
public static void tweakWebCoredump(WebView webvew) { try { Class<?> clzz = Class.forName("android.webkit.WebView"); Field f = clzz.getDeclaredField("mProvider"); f.setAccessible(true); Object mProvider = f.get(webvew); Class<?> WebViewClassic = Class .forName("android.webkit.WebViewClassic"); WebViewClassic.cast(mProvider); Field f2 = WebViewClassic.getDeclaredField("mWebViewCore"); f2.setAccessible(true); Object mWebViewCore = f2.get(mProvider); Method sendMessage = Class.forName("android.webkit.WebViewCore") .getDeclaredMethod("sendMessage", int.class, int.class, int.class); sendMessage.setAccessible(true); // 通过反射获取WebViewCore对象的sendMessage的方法,170和171分别代表 // 答应DomTree和RenderTree的方法 sendMessage.invoke(mWebViewCore, 170, 1, 0); sendMessage.invoke(mWebViewCore, 171, 1, 0); // Method nativeDumpDisplayTree = WebViewClassic // .getDeclaredMethod("nativeDumpDisplayTree", String.class); // nativeDumpDisplayTree.setAccessible(true); // nativeDumpDisplayTree.invoke(mProvider, webvew.getUrl()); } catch (Throwable e) { TaoLog.e("TweakWebCoreHandler", "tweakWebCoreHandler exception: " + e); } }
Android 4.4 及以上的(敬请期待……)