我已经使用缓存机制实现了webview,该机制在设备脱机时从缓存加载页面。但奇怪的是我得到了以下行为。
每当设备离线且app正在从缓存加载页面时,它看起来像预期的那样:
但下次我打开同一页面的webview时。它从缓存中呈现页面而不使用css:
序列继续其他时间.i.e首先是预期的页面,另一次是没有css的页面。我试图找到解决方案,但未能提出任何解决方案。有谁知道webview加载的奇怪行为的原因?
我的布局文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
我在framelayout中声明了webview:
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT>=21) {
WebView.enableSlowWholeDocumentDraw();
}
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
}
catch (Exception e)
{
Toast.makeText(DisplayResult.this, ""+e, Toast.LENGTH_SHORT).show();
}
}
public void initUI()
{
try {
webViewPlaceholder = (FrameLayout) findViewById(R.id.webViewPlaceholder);
if (webView == null) {
//webView = (WebView) findViewById(R.id.webview);
webView = new WebView(this);
// webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
webView.getSettings().setTextZoom(0);
webView.setWebViewClient(new Callback());
if(!isConnectedToInternet())
{
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
try {
webView.loadUrl(url.toString());
}
catch(Exception e)
{
Toast.makeText(DisplayResult.this,""+e,Toast.LENGTH_LONG).show();
}
}
webViewPlaceholder.addView(webView);
}
catch (Exception e)
{
Toast.makeText(DisplayResult.this, ""+e, Toast.LENGTH_SHORT).show();
}
}
private class Callback extends WebViewClient {
MaterialDialog m=null ;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
//Toast.makeText(DisplayResult.this,"onload "+ (i++),Toast.LENGTH_SHORT).show();
if(flag && m==null) {
m = new MaterialDialog.Builder(DisplayResult.this)
.title("Fetching Result")
.content("Please wait...")
.progress(true, 0)
.show();
}
}
@Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view,url);
//Toast.makeText(DisplayResult.this,"onfinish",Toast.LENGTH_SHORT).show();
if(flag || m!=null) {
m.cancel();
//Toast.makeText(DisplayResult.this, "canceled", Toast.LENGTH_SHORT).show();
}
i=1;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try {
//Toast.makeText(DisplayResult.this,"onerror",Toast.LENGTH_SHORT).show();
new MaterialDialog.Builder(DisplayResult.this)
.title("Error ")
.content("No Internet Access")
.positiveText("OK")
.show();
if (errorCode == -2) {
String summary = "<html><body ><p style='color: red;'>Unable to load information. Please check if your network connection is working properly or try again later.</p></body></html>";
view.loadData(summary, "text/html", null);
}
}
catch (Exception e)
{
Toast.makeText(DisplayResult.this, ""+e, Toast.LENGTH_SHORT).show();
}
}
}
我的应用程序的Targetsdkversion是21.任何帮助将不胜感激。