2011-09-21 144 views
0

我正在開發一個使用webview顯示網頁的Android應用程序。我的大部分代碼都與webview有關。我的主要活動包含一個顯示特定網站的webview。Android上的Webview數據庫損壞

我試圖防止當我的webview.db損壞的錯誤。我知道這不是一種常見的情況,但我想確保我的應用程序不會崩潰。

嘗試訪問網絡視圖數據庫時,它已被損壞 將導致崩潰。

我添加了方法setUncaughtExceptionHandler來處理異常。我可以捕獲的豁免,但是當我試圖重新啓動我的應用程序時,webview永遠不會完成加載。

我嘗試了後續代碼爲「重新啓動」我的應用程序:

 Intent i = new Intent(context, Error.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(i); 

我最後一次嘗試未成功,然後我添加了顯示錯誤信息,刪除的WebView數據庫和關閉應用程序的一些代碼。

AlertDialog alertDialog = new AlertDialog.Builder(
        DiscoverMobile.this).create(); 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage("Application files have been deleted or corrupted. The app will close to fix this issue. You can restart the app later"); 
      alertDialog.setButton("OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 


          webview.getContext().deleteDatabase(
            "webview.db"); 
          webview.getContext().deleteDatabase(
            "webviewCache.db"); 
          WebViewDatabase webViewDB = WebViewDatabase 
            .getInstance(getBaseContext()); 

          System.exit(0); 
         } 
        }); 
      alertDialog.show(); 

這是一個很好的解決方案嗎?

回答

1
Override 
public void onPageFinished(WebView view, String url) { 
    super.onPageFinished(view, url); 
    view.clearCache(true); 
} 

也許你試過這個,但也許也可以設置WebView緩存大小爲小。我不知道是否0會工作,所以也許1:

webview.getSettings().setAppCacheMaxSize(1);