我試圖從使用JavaScript的網站檢索html。我的方法如下:ART在加載完成之前殺死WebView
- 創建一個無頭WebView(沒有在XML中定義任何視圖)並啓用JS。
- 使用要在加載完成時調用的方法創建JavaScriptInterface。
- 覆蓋我提供的WebClientView的onPageFinished方法。這裏的代碼注入JavaScript來調用我在JavaScriptInterface中定義的方法,將頁面的html內容作爲參數傳遞。
有趣的是,此代碼的工作,但不一致。有一次它會像預期的那樣工作,下次不會發生任何事情。我收到各種奇怪的logcat消息,其中一個我懷疑可能是一個不好的標誌:W/art: Attempt to remove non-JNI local reference, dumping thread
。 今天想到一個想法:也許有時我的WebView被殺死,因爲運行時環境做了優化(並且我的WebView沒有鏈接到它的XML視圖對象)?
所以,我決定嘗試建立在我的XML網頁視圖,並使用相同的代碼用它,而不是隻建立一個代碼的。現在代碼一直工作,每次運行它,我不再收到這些線程轉儲日誌消息。只要我看到頁面加載完成,代碼就會被執行。
請注意,我注意到這個特定的網站我引用需要相對較長的時間來加載(只要幾秒鐘),並且onPageFinished被多次調用,提供未經處理的JS的HTML,除非它是最後一次通話。
所以,我的問題是:有沒有什麼辦法可以防止從ART使用無頭版本(或任何其他解決方法)傾倒時我的線程?我想保留在XML中定義的WebView,並將它的可見性設置爲false,但它似乎是一個資源密集型解決方案。
我正在與Android模擬器工作(雖然我使用的物理設備,以及相同的結果),目標API爲24,最低API 15.
相關的代碼:
private void fetchHTML(View view, String url) {
class JSInterface{
@JavascriptInterface
public void processHTML(String html){
Log.d(LOG_TAG, html); // This bit of code sometimes gets executed, and sometimes does not
}
}
//final WebView webView = (WebView) view.findViewById(R.id.webview);
final WebView webView = new WebView(context);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.addJavascriptInterface(new JSInterface(), "Android");
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(webView.getProgress() == 100){
webView.loadUrl("javascript:window.Android.processHTML("
+ "'<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
}
});
settings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
無法正常執行時相關的logcat:
07-15 17:16:27.616 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /data/app/com.michaelsvit.kolnoa-2/lib/x86
07-15 17:16:28.214 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /data/app/com.michaelsvit.kolnoa-2/lib/x86
07-15 17:16:28.267 17779-17779/com.michaelsvit.kolnoa W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-15 17:16:28.448 17779-17779/com.michaelsvit.kolnoa I/WebViewFactory: Loading com.android.webview version 44.0.2403.119 (code 246011910)
07-15 17:16:28.456 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /system/app/webview/lib/x86
07-15 17:16:28.465 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Time to load native libraries: 3 ms (timestamps 8656-8659)
07-15 17:16:28.465 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Expected native library version number "",actual native library version number ""
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {c4bffce}
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Expected native library version number "",actual native library version number ""
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa I/chromium: [INFO:library_loader_hooks.cc(120)] Chromium logging enabled: level = 0, default verbosity = 0
07-15 17:16:28.476 17779-17779/com.michaelsvit.kolnoa I/BrowserStartupController: Initializing chromium process, singleProcess=true
07-15 17:16:28.479 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.479 17779-17779/com.michaelsvit.kolnoa E/SysUtils: ApplicationContext is null in ApplicationStatus
07-15 17:16:28.484 17779-17779/com.michaelsvit.kolnoa W/chromium: [WARNING:resource_bundle.cc(285)] locale_file_path.empty()
07-15 17:16:28.487 17779-17779/com.michaelsvit.kolnoa E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
07-15 17:16:28.487 17779-17779/com.michaelsvit.kolnoa E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
[ 07-15 17:16:28.488 17779:17779 D/ ]
HostConnection::get() New Host Connection established 0xac3edff0, tid 17779
07-15 17:16:28.525 17779-17779/com.michaelsvit.kolnoa E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008d57
07-15 17:16:28.552 17779-17779/com.michaelsvit.kolnoa E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
07-15 17:16:28.568 17779-17831/com.michaelsvit.kolnoa W/AudioManagerAndroid: Requires BLUETOOTH permission
07-15 17:16:28.578 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.582 17779-17779/com.michaelsvit.kolnoa W/AwContents: onDetachedFromWindow called when already detached. Ignoring
07-15 17:16:28.584 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.585 17779-17779/com.michaelsvit.kolnoa W/AwContents: onDetachedFromWindow called when already detached. Ignoring
07-15 17:16:28.587 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.587 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.620 17779-17841/com.michaelsvit.kolnoa D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-15 17:16:28.639 17779-17829/com.michaelsvit.kolnoa W/chromium: [WARNING:data_reduction_proxy_config.cc(423)] SPDY proxy OFF at startup
07-15 17:16:28.740 17779-17841/com.michaelsvit.kolnoa I/OpenGLRenderer: Initialized EGL, version 1.4
[ 07-15 17:16:28.741 17779:17841 D/ ]
HostConnection::get() New Host Connection established 0xa0a5f8d0, tid 17841
07-15 17:16:28.782 17779-17841/com.michaelsvit.kolnoa W/EGL_emulation: eglSurfaceAttrib not implemented
07-15 17:16:28.782 17779-17841/com.michaelsvit.kolnoa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa3d9180, error=EGL_SUCCESS
[ 07-15 17:16:28.815 17779:17850 D/ ]
HostConnection::get() New Host Connection established 0xa0c82c80, tid 17850
07-15 17:16:29.166 17779-17779/com.michaelsvit.kolnoa W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 17779
07-15 17:16:29.279 17779-17779/com.michaelsvit.kolnoa D/MovieGridFragment:
07-15 17:16:29.768 17779-17779/com.michaelsvit.kolnoa W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 17779
07-15 17:16:30.894 17779-17779/com.michaelsvit.kolnoa I/chromium: [INFO:CONSOLE(2)] "Uncaught TypeError: Cannot read property 'length' of undefined", source: (2)
07-15 17:16:31.196 17779-17829/com.michaelsvit.kolnoa W/chromium: [WARNING:spdy_session.cc(2527)] Received WINDOW_UPDATE for invalid stream 1