San*_*fer 6 android caching cordova
老实说,我不确定是否应该将其发布在SO上; 无论哪种方式,让我们互相帮助.
我正在建立一个网络应用程序,我定期在我的Android手机上查看.我没有将其上传到Phonegap或其他任何东西,而是配置了一个简单的页面,其中iFrame指向Web应用程序的内容(在线托管).
糟糕的是:为了查看更改,我必须清理应用缓存.否则,'之前'版本仍然显示(因为它卡在缓存中).
所以我希望是否有一个选项可以在页面内打开/关闭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) | | | |
| | | | | | | |
| | | ------------------------- | | |
| | | | | |
| | | | | |
| | --------------------------------- | |
| | | |
| ---------------------------------------- |
| |
-------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
gok*_*urt 10
有两种方法可以禁用Cordova/Phonegap应用程序上的缓存.
我将详细描述这两个选项.
适用于Cordova的新版本(5.3.3)
添加以下导入
import android.webkit.WebSettings;
import android.webkit.WebView;
Run Code Online (Sandbox Code Playgroud)
像这样覆盖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
}
Run Code Online (Sandbox Code Playgroud)
=======
适用于旧版Cordova
假设您正在Activity
课堂上加载您的内容.
您可以在将Web视图加载到Activity类时配置它.
以下是您可以了解如何在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");
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,此代码块将在onResume()
触发事件时加载内容,这意味着只要您的应用位于前台,您的Web内容就会重新加载.
下面的代码阻止了webview上的缓存.
super.appView.getSettings().setAppCacheEnabled(false);
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Run Code Online (Sandbox Code Playgroud)
这个解决方案真的很傻,但表现得像预期的那样.对于您的情况,它可能会有所帮助.
您只需在网址末尾添加时间戳值即可.以下是示例代码段.
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());
}
}
Run Code Online (Sandbox Code Playgroud)
它会在您的网址末尾添加时间戳值,并将内容加载为新内容.
这些是避免在Web应用程序中缓存的两种方法Phonegap/Cordova
.
希望这可能会有所帮助.