问题
I need to load an HTML5 / Javascript game into a WebView.
Since this works on an old phone running Gingerbread but fails on a Nexus7 running KitKat, I'm guessing maybe it's something to do with the new Chromium based WebView in Android 4.4? The game works fine in the Chrome Browser on the Nexus7, but not in the WebView in my app.
Any advice where I should be looking to track down the source of this problem would be greatly appreciated.
The LogCat when I run on the Nexus7 is:
05-29 13:49:46.421 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GameActivity﹕ Logged into Facebook
05-29 13:49:46.421 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GameActivity﹕ mNewCheckin = false
05-29 13:49:47.371 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GameActivity﹕ checkInToPlay()
05-29 13:49:47.371 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GameActivity﹕ Showing Game
05-29 13:49:47.371 2526-2526/au.com.xxxxxxx.xxxxxxxxxx V/WebViewChromium﹕ Binding Chromium to the background looper Looper (main, tid 1) {4204a888}
05-29 13:49:47.371 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
05-29 13:49:47.381 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/BrowserProcessMain﹕ Initializing chromium process, renderers=0
05-29 13:49:47.381 2526-2563/au.com.xxxxxxx.xxxxxxxxxx W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
05-29 13:49:47.421 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ Loading URL into WebView
05-29 13:49:47.491 2526-2526/au.com.xxxxxxx.xxxxxxxxxx W/AwContents﹕ nativeOnDraw failed; clearing to background color.
05-29 13:49:47.501 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ Page Started Loading...
05-29 13:49:47.501 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageStarted:WebView ID: 2131296360
05-29 13:49:47.501 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageStarted:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:47.621 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:CONSOLE(9)] "Viewport argument key "minimal-ui" not recognized and ignored.", source: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html (9)
05-29 13:49:48.951 2526-2540/au.com.xxxxxxx.xxxxxxxxxx W/ContextImpl﹕ Implicit intents with startService are not safe: Intent { act=com.google.android.gms.analytics.service.START (has extras) } android.content.ContextWrapper.bindService:517 com.google.analytics.tracking.android.AnalyticsGmsCoreClient.connect:82 com.google.analytics.tracking.android.GAServiceProxy.connectToService:330
05-29 13:49:48.951 2526-2540/au.com.xxxxxxx.xxxxxxxxxx W/GAV3﹕ Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
05-29 13:49:48.961 2526-2540/au.com.xxxxxxx.xxxxxxxxxx I/GAV3﹕ Thread[GAThread,5,main]: No campaign data found.
05-29 13:49:49.381 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ ...Page Finished Loading
05-29 13:49:49.381 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:WebView ID: 2131296360
05-29 13:49:49.381 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:49.411 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
05-29 13:49:49.431 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
05-29 13:49:49.681 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:CONSOLE(153)] "Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.", source: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/c2runtime.js (153)
05-29 13:49:49.781 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ ...Page Finished Loading
05-29 13:49:49.781 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:WebView ID: 2131296360
05-29 13:49:49.781 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:49.931 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ ...Page Finished Loading
05-29 13:49:49.931 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:WebView ID: 2131296360
05-29 13:49:49.931 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:49.971 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ ...Page Finished Loading
05-29 13:49:49.971 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:WebView ID: 2131296360
05-29 13:49:49.971 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:49.991 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
05-29 13:49:50.081 2526-2601/au.com.xxxxxxx.xxxxxxxxxx E/MediaResourceGetter﹕ Invalid url: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
05-29 13:49:50.081 2526-2564/au.com.xxxxxxx.xxxxxxxxxx E/MediaResourceGetter﹕ Invalid url: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
05-29 13:49:50.081 2526-2600/au.com.xxxxxxx.xxxxxxxxxx E/MediaResourceGetter﹕ Invalid url: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
05-29 13:49:50.131 2526-2564/au.com.xxxxxxx.xxxxxxxxxx E/MediaResourceGetter﹕ Invalid url: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
05-29 13:49:50.151 2526-2601/au.com.xxxxxxx.xxxxxxxxxx E/MediaResourceGetter﹕ Invalid url: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
05-29 13:49:50.231 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ ...Page Finished Loading
05-29 13:49:50.231 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:WebView ID: 2131296360
05-29 13:49:50.231 2526-2526/au.com.xxxxxxx.xxxxxxxxxx D/GamePlayFragment﹕ onPageFinished:URL: http://www.xxxxxxx.com.au/xxxxxx/xxx/xxxxxxxxx/xxxxx/xxx/index.html
05-29 13:49:51.011 2526-2526/au.com.xxxxxxx.xxxxxxxxxx I/chromium﹕ [INFO:CONSOLE(1)] "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.", source: https://www.facebook.com/connect/ping?client_id=.........
05-29 13:49:53.961 2526-2592/au.com.xxxxxxx.xxxxxxxxxx W/ContextImpl﹕ Implicit intents with startService are not safe: Intent { act=com.google.android.gms.analytics.service.START (has extras) } android.content.ContextWrapper.bindService:517 com.google.analytics.tracking.android.AnalyticsGmsCoreClient.connect:82 com.google.analytics.tracking.android.GAServiceProxy.connectToService:330
05-29 13:49:53.971 2526-2592/au.com.xxxxxxx.xxxxxxxxxx W/GAV3﹕ Thread[Service Reconnect,5,main]: Service unavailable (code=1), using local store.
05-29 13:49:54.001 2526-2592/au.com.xxxxxxx.xxxxxxxxxx D/dalvikvm﹕ GC_FOR_ALLOC freed 1133K, 11% free 10204K/11372K, paused 26ms, total 27ms
Here's how I'm currently implementing the WebView.
// webview
final WebView webView = (WebView)view.findViewById(R.id.game_webview);
webView.setWebChromeClient(new WebChromeClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.loadUrl("http://www.xxxxxxx.xxx.xx/xxxxx/xxx/index.html");
来源:https://stackoverflow.com/questions/23932708/loading-html5-js-into-webview-fails-on-nexus7-running-kitkat-but-ok-on-phone-ru