我尝试使用WebView加载URL,但Web视图似乎不稳定 - 有时页面加载100%,有时它会停止加载30%/ 50%/ 75%(随机)。 我在Android Bolts任务中创建了WebView,并且没有为用户显示WebView,它只会为将来的请求加载一些javascript。
代码:
private Task<Boolean> task_SetupJavascript(){
final Task<Boolean>.TaskCompletionSource myTask = Task.create();
WebView webview = new WebView(getActivity());
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: LocalStorage.set('valueA', 'valueB', 'valueC');");
myTask.setResult(Boolean.TRUE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// never fired during tests
Log.e(TAG,"onReceivedError ---> Something went wrong... ");
myTask.setCancelled();
}
});
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.e(TAG, ""+newProgress);
}
});
webview.loadUrl(URL);
webview.onResume();
return myTask.getTask();
}
我无法弄清楚是什么导致WebView在进度中间停止随机加载。 有时它在第一次运行此任务时失败,有时在3/4/5时间后失败。