老实说,我不确定我是否应该在 SO 上发布它;无论哪种方式,让我们互相帮助。
此时,我正在构建一个网络应用程序,我会定期在我的 Android 手机上检查它。我没有一直将它上传到 Phonegap 或其他东西,而是配置了一个简单的页面,其中的 iFrame 指向网络应用程序的内容(在线托管)。
这样做的坏处:为了查看更改,我必须清理应用程序缓存。否则,“以前的”版本仍在显示(因为它卡在缓存中)。
所以我希望是否有一个选项可以在页面中打开/关闭 Android/Configure 以关闭所有对象和文件的缓存?
非常感谢你们!
了解我的工作方式..
-------------------------------------------------
| My Phone |
| With a |
| |
| ----------------------------------------- |
| | Cordova/Phonegap | |
| | Application which | |
| | loads a | |
| | | |
| | -------------------------------- | |
| | | Website with | | |
| | | iFrame | | |
| | | height:100% | | |
| | | width:100% | | |
| | | | | |
| | | ------------------------- | | |
| | | | | | | |
| | | | HTML5 | | | |
| | | | Responsive | | | |
| | | | Webpage | | | |
| | | | (The WebApp itself) | | | |
| | | | | | | |
| | | ------------------------- | | |
| | | | | |
| | | | | |
| | --------------------------------- | |
| | | |
| ---------------------------------------- |
| |
-------------------------------------------------
最佳答案
有两种方法可以在 Cordova/Phonegap 应用程序上禁用缓存。
- 第一个是在加载您的内容时配置 WebView 设置。
- 第二个是在您每次要刷新页面时向您的网址添加一个时间戳值。这更有可能是一种解决方法。
我将详细描述这两个选项。
第一个解决方案
对于新版本的 Cordova (5.3.3)
添加以下导入
import android.webkit.WebSettings;
import android.webkit.WebView;
像这样覆盖 onResume-
@Override
protected void onResume() {
super.onResume();
// Disable caching ..
WebView wv = (WebView) appView.getEngine().getView();
WebSettings ws = wv.getSettings();
ws.setAppCacheEnabled(false);
ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
loadUrl(launchUrl); // launchUrl is the default url specified in Config.xml
}
=======
对于旧版本的 Cordova
假设您要在 Activity
类中加载内容。
您可以在 webview 加载到 Activity 类时对其进行配置。
这是示例代码 fragment ,您可以了解如何在 Phonegap/Cordova
应用程序中禁用浏览器缓存。
public class MainActivity extends DroidGap {
@Override
protected void onResume() {
super.onResume();
// Disable caching ..
super.appView.getSettings().setAppCacheEnabled(false);
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
super.loadUrl("http://blabla.com");
}
}
如您所见,只要触发 onResume()
事件,此代码块就会加载内容,这意味着只要您的应用程序在前台运行,您的 Web 内容就会重新加载。
下面的一段代码可以防止在 webview 上缓存。
super.appView.getSettings().setAppCacheEnabled(false);
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
第二种解决方案
这个解决方案确实很愚蠢,但表现符合预期。对于您的情况,这可能会有所帮助。
您可以简单地在 url 末尾添加时间戳值。这是示例代码 fragment 。
public class MainActivity extends DroidGap {
@Override
protected void onResume() {
super.onResume();
StringBuilder urlBuilder = new StringBuilder("http://blabla.com");
urlBuilder.append("?timestamp=");
urlBuilder.append(new Date().getTime());
super.loadUrl(urlBuilder.toString());
}
}
它每次都会在您的网址末尾附加时间戳值并加载新内容。
这是避免在 Phonegap/Cordova
的网络应用程序中缓存的两种方法。
希望这对您有所帮助。
关于android - 在 Android 上为 WebApps 关闭缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221395/