Android 上的 Webview 数据库损坏

发布于 2024-12-06 03:21:53 字数 1678 浏览 1 评论 0原文

我正在开发一个 Android 应用程序,它使用 webview 来显示网页。我的大部分代码都与 webview 相关。我的主要活动包含一个显示特定网站的网络视图。

当我的 webview.db 损坏时,我试图防止出现错误。我知道这并不常见,但我想确保我的应用程序不会崩溃。

尝试访问 webview 数据库 损坏将导致崩溃。

我添加了 setUncaughtExceptionHandler 方法来处理异常。我可以捕获异常,但是当我尝试重新启动应用程序时,网络视图永远不会完成加载。

我尝试使用以下代码来“重新启动”我的应用程序:

     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();

这可能是一个好的解决方案吗?

I'm developing an Android app which uses a webview to display a webpage. Most of my code is related to the webview. My main activity contains a webview which displays an specific web site.

I'm trying to prevent an error when my webview.db is corrupted. I know that is not a common situation but I would like to make sure that my app will not crash.

Attempting to access the webview database when it has been
corrupted will result in a crash.

I added the method setUncaughtExceptionHandler to handle the exception. I can catch the exeption but when I tried to restart my app the webview never finishes loading.

I tried the follow code to "restart" my app:

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

My last try was unsuccessfully then I added some code which displays an error message, removes the webview databases and closes the app.

    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();

Could this be a good solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-12-13 03:21:53
Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    view.clearCache(true);
}

也许您尝试过此操作,但也可能将 WebView 缓存大小设置为较小的值。我不确定 0 是否有效,所以也许 1:

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

Maybe you tried this, but maybe also set the WebView Cache size to something small. I'm not sure if 0 will work, so maybe 1:

webview.getSettings().setAppCacheMaxSize(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文