Android 上的 Webview 数据库损坏
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您尝试过此操作,但也可能将 WebView 缓存大小设置为较小的值。我不确定 0 是否有效,所以也许 1:
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: