在Android上关闭WebApps的缓存 [英] Turn off caching on Android for WebApps

查看:240
本文介绍了在Android上关闭WebApps的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,我不确定是否应该在SO上发布;

Honestly, I'm unsure whether I should post this on SO; either way, let's help each other out.

我正在建立一个网络应用程序,我定期检查我的Android手机,在这一点上。

I'm building a web app which I regularly check on my Android phone, at this point. Instead of uploading it to Phonegap or something all the time, i've configured a simple page with an iFrame pointing to the content of the web app (hosted online).

这是一个简单的页面,它包含一个iFrame指向网络应用程序的内容(在线托管)。坏事:为了看到更改,我必须清理应用缓存。否则,'上一个'版本仍然显示(因为它停留在缓存)。

The bad thing of this: In order to see the changes, I have to clean the app cache. Else, the 'previous' version is still showing (because it's stuck in the cache).

所以我希望如果有一个选项,打开/关闭Android /

So I was hoping if there was an option to turn on/off on Android/Configure within the page that turns off caching for ALL the objects and files?

感谢很多人!

 -------------------------------------------------
|                   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应用程序。

There are two ways to disable caching on Cordova/Phonegap apps.


  1. 第一个是在载入内容时设定网路检视设定。

  2. 时间戳值到你的url每次你想刷新页面。这更有可能是一种解决方法。

我将详细描述这两个选项。

I'll describe both options in detail.

新版Cordova(5.3.3)

/ p>

Add below imports

import android.webkit.WebSettings;
import android.webkit.WebView;

覆盖onResume -

Override onResume like this-

@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

在您的活动课程上载入您的内容。

Assuming that you're loading your content on your Activity class.

您可以在Web视图加载到您的Activity类时配置它。

You are able to configure your webview while it's loaded on your Activity class.

以下是范例程式码片段,您可以了解如何在 Phonegap / Cordova 应用程式中停用浏览器快取。

Here is the sample code snippet that you can understand how to disable browser caching in Phonegap/Cordova apps.

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()事件,这意味着每当您的应用处于前台时,您的网络内容将被重新加载。

As you can see, this code block will load content whenever onResume() event is triggered which means that your web content will be reloaded whenever your app is in the foreground.

下面的代码阻止在webview上缓存。

Following piece of code prevents caching on webview.

super.appView.getSettings().setAppCacheEnabled(false); 
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);



第二个解决方案



愚蠢,但表现得如预期。对于您的情况,这可能会有所帮助。

Second Solution

This solution is really silly but behaves as expected. For your situation, it may be helpful.

您可以在网址末尾添加时间戳记值。这是示例代码片段。

You can simply add timestamp value at the end of the url. Here is the sample code snippet.

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

These are the two ways to avoid caching in web apps in Phonegap/Cordova.

希望这可能会有帮助。

这篇关于在Android上关闭WebApps的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆