清除用户数据或在Phonegap上清除缓存android
问题描述:
如何使用PhoneGap和Android清除用户数据或清除缓存?下面的代码不起作用。我应该在哪里进行更改;在HTML端还是在Java端?另外我正在访问一个AJAX请求,并在PUT方法的第二次尝试中,数据不会更新,所以我的主要嫌疑人是Android存储的用户数据。我在我的AJAX请求中添加了cache: false
以确保它不存储缓存。但它仍然不起作用。有想法该怎么解决这个吗?清除用户数据或在Phonegap上清除缓存android
我有这个代码基于另一个问题。
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
所以基本上我的POSActivity.java看起来像这样。
public class PocActivity extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.setIntegerProperty("splashscreen", R.drawable.is);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
deleteCache(this);
super.loadUrl("file:///android_asset/www/index.html");
}
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
}
答
添加下面的代码在你的OnCreate活动文件中的()方法加载的index.html后:
if (super.appView != null) {
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
答
我一直在寻找一种简单的方法来清除缓存在我的科尔多瓦应用,并发现this plugin使用下面的代码清除在Android的web视图缓存:
// clear the cache
self.webView.clearCache(true);
而在iOS上,在后台运行,并做到这一点:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
使用插件,只需拨打window.CacheClear(success, error);
幸运的是,它解决了我的问题。 非常感谢这位cordova-plugin-cache-clear的作者!
我会开始调试AJAX放置请求本身,因为使用缓存:false,你永远不会得到相同的响应两次 - jQuery自动添加一个时间戳到这样的请求,防止它被缓存 – 2012-08-14 10:38:24
...另外,我也有成功地使用了上面提到的代码来清除Android设备上的内部缓存,所以我猜这些代码工作得很好 – 2012-08-14 10:39:31