以编程方式清除 Android 上的 PhoneGap/Cordova 应用程序缓存以模拟全新安装? [英] Programmatically clear PhoneGap/Cordova app's cache on Android to simulate a fresh install?
问题描述
这与我之前的如何在每次安装时清除 Android 模拟器上应用的 localStorage?'.
它还建立在 '我如何清除 Android 应用程序缓存?' 和 '如何以编程方式清除应用程序数据?'.
It also builds on 'How can I clear the Android app cache?' and 'How to programatically clear application data?'.
以上问题都没有给出适用于 Android PhoneGap/Cordova 应用程序的直接答案.这篇博文 by Igor Hrupin 介绍了原生 Android 应用,所以这个问题扩展到涵盖 Cordova.
None of the above questions give a straightforward answer that is applicable to Android PhoneGap/Cordova applications. This blog post by Igor Hrupin covers the situation in the context of a native Android app, so this question extends that to cover Cordova.
我会自己发布一个答案,但我完全是一个 Java 菜鸟,所以请编辑以改进.
I will post an answer myself, but I am a total Java noob, so please edit to improve.
推荐答案
我无法强制 android 重新加载新版本:
I wasn't able to force android to reload new versions of:
super.loadUrl("file:///android_asset/www/index.html");
在调用之间,所以我首先借用了你的代码,这些代码运行良好,只是我的应用程序正在写入的所有数据也被删除了.
between calls, so I first borrowed your code which worked well enough except that all data that my application was writing was deleted as well.
一个快速而简单的解决方法是更改上面文件的 URL:
A quick and simple fix is to change the URL of the file above:
super.loadUrl("file:///android_asset/www/index.html?1");
并更改 ? 后面的数字.每个负载.最后我补充说:
And change the number behind the ? with each load. Eventually I added:
import java.util.Random;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
Random generator = new Random();
int r = generator.nextInt();
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html?" + r);
}
}
现在,每次我重建应用程序时,它实际上都会刷新 html 文件的缓存版本.
Now each time I rebuild the app it actually refreshes the cached version of the html file.
我非常想知道如何强制 loadUrl 从应用程序内部刷新,或强制重新加载.
I'd very much like to know how to force loadUrl into a refresh from within the app itself, or to force it on reload.
这篇关于以编程方式清除 Android 上的 PhoneGap/Cordova 应用程序缓存以模拟全新安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!