删除android webview的数据库,webview本地存储需要杀掉进程才能生效
我需要区分不同用户的webview数据,所以我需要备份webview数据并还原它,但是webview重新加载页面后还原的数据不符合最新的,需要杀掉进程读取最新的数据,应该解决杀掉进程的问题删除android webview的数据库,webview本地存储需要杀掉进程才能生效,android,webview,Android,Webview,我需要区分不同用户的webview数据,所以我需要备份webview数据并还原它,但是webview重新加载页面后还原的数据不符合最新的,需要杀掉进程读取最新的数据,应该解决杀掉进程的问题 private const val WEB_VIEW_DATA_DIR_NAME = "webview" private const val WEB_VIEW_CACHE_DIR_NAME = "WebView" private const val WEB_VIEW_U
private const val WEB_VIEW_DATA_DIR_NAME = "webview"
private const val WEB_VIEW_CACHE_DIR_NAME = "WebView"
private const val WEB_VIEW_USER_INFO_NAME = "app_user_id.txt"
@JvmStatic
fun checkWebData(context: Context, userId: String) {
backupWebView(context, userId)
backupWebViewCache(context, userId)
}
private fun backupWebViewCache(context: Context, userId: String) {
val dir = File(context.cacheDir, WEB_VIEW_CACHE_DIR_NAME)
if (dir.exists()) {
backupDir(userId, dir) {
}
}
}
private fun backupWebView(context: Context, userId: String) {
val dir = context.getDir(WEB_VIEW_DATA_DIR_NAME, Context.MODE_PRIVATE)
if (!dir.exists()) {
dir.mkdirs()
}
backupDir(userId, dir) {
//临时方案,需解决web view数据库文件修改后,需杀进程才能生效问题
WebStorage.getInstance().deleteAllData()
}
}
private fun backupDir(userId: String, dir: File, function: (Boolean) -> Unit) {
val userFile = File(dir, WEB_VIEW_USER_INFO_NAME)
if (userFile.exists()) {
val readFileToString = readFileToString(userFile)
if (readFileToString != userId) {
function.invoke(dir.renameTo(File("${dir.absolutePath}_$readFileToString")))
val file = File("${dir.absolutePath}_$userId")
if (file.exists()) {
val renameTo = file.renameTo(dir)
LogUtils.d(TAG, "restore ${dir.name} result : $renameTo")
}
}
} else {
writeStringToFile(userId, userFile)
}
}
private fun readFileToString(file: File) : String {
var fileInputStream : FileInputStream? = null;
try {
fileInputStream = FileInputStream(file)
return String(fileInputStream.readBytes())
} catch (e: Exception) {
} finally {
try {
fileInputStream?.close()
} catch (e: Exception) {
}
}
return ""
}
private fun writeStringToFile(string: String, file: File) {
var fileOutputStream : FileOutputStream? = null
try {
fileOutputStream = FileOutputStream(file)
file.writeBytes(string.toByteArray())
} catch (e: Exception) {
} finally {
try {
fileOutputStream?.close()
} catch (e: Exception) {
}
}
}