更新的WebView我有一个应用程序,从网络上获得一个XML文件,对其进行解析中检索路径的网站我有一个网页,以cache.eg如何从的AsyncTask
/services/orthopaedics.php
。 我可以成功下载需要缓存的所有页面的路径。目前我在AsyncTask中执行此操作。 Web服务调用(页面路径)的结果存储在AsyncTask的doInBackground方法中的contentsValues中。然后我遍历onPostExecute()方法中的这些contentsvalues,我可以逐个加载webview的loadUrl()方法中的网页。这个webview是隐形的,因为它只用于缓存目的。
问题是,因为不可见的web视图正在加载用户可以看到的另一个webview,并且显示网站的索引页面,所以加载速度很慢。出现一个白色屏幕,直到不可见的webview已停止加载缓存。在phoe上,大约需要4秒,这是可以接受的,但是在android平板电脑上大约需要30秒。
我知道调用webview必须在UI线程上,这就是为什么我加载onPostExecute方法中的网页(隐形webview)。是否有可能以某种方式将页面加载到doInBacground方法中的缓存中,但将webview.loadUrl()放入runOnUiThread(新的Runnable)中?
我会给我一个简单的样本,我的意思如下。
private class AsyncCacheSite extends AsyncTask<String, ContentValues, ContentValues>{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e(TAG, "about to load main page");
webView.loadUrl(mobileWebsiteUrl, getHeaders());
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Connecting to Server");
progressDialog.setMessage("Caching website for offline use...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected ContentValues doInBackground(String... params) {
ContentValues cv = null;
try {
//call the service to get the xml file containing paths
cv = ws.checkForUpdates();
} catch (Exception e) {
e.printStackTrace();
}
//iterate through the xml file geting the paths
.......
.......
.......
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
loadUrlIntoCache(page);
}
})
return null;
}
@Override
protected void onPostExecute(ContentValues result) {
super.onPostExecute(result);
progressDialog.dismiss();
}//end of postExecute
}//end of asyncTask
public void loadUrlIntoCache(String pageUrl){
WebView wv2 = new WebView(MainActivity.this);
wv2.getSettings().setSupportZoom(true);
wv2.getSettings().setBuiltInZoomControls(true);
wv2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv2.setScrollbarFadingEnabled(true);
wv2.getSettings().setLoadsImagesAutomatically(true);
wv2.getSettings().setDomStorageEnabled(true);
wv2.getSettings().setAppCacheEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
wv2.getSettings().setAppCacheMaxSize(1024*1024*32);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
wv2.getSettings().setAppCachePath(appCachePath);
wv2.getSettings().setAllowFileAccess(true);
wv2.getSettings().setJavaScriptEnabled(true);
// wv2.setWebViewClient(new WebViewClient());
////////////////////////////////////////////////////////////////////////////////////////
/////the webviewclient below is the code you gave me to overcome the redirecting issue//
////////////////////////////////////////////////////////////////////////////////////////
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
wv2.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wv2.setVisibility(View.GONE);
wv2.loadUrl(mobileWebsiteUrl + pageUrl, getHeaders());
Log.e(TAG, "loading " + mobileWebsiteUrl + pageUrl);
}
你能告诉我该怎么做 – mohitum 2014-02-13 13:40:34