javascript 如何永久禁用 Android 浏览器缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5210808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:22:56  来源:igfitidea点击:

How to disable the Android browser cache permanently?

javascriptandroidandroid-emulatorcache-control

提问by Chris

I'm developing a web based application that also should run on Android based phones. As I don't have one I'm successfully using the emulator from the SDK.

我正在开发一个基于 Web 的应用程序,它也应该在基于 Android 的手机上运行。因为我没有,所以我成功地使用了 SDK 中的模拟器。

But as I'm constantly changing some JavaScript pages of the application the browser uses the old versions out of it's cache (cache control on the server is right - but I'm not having there the normal use case where excessive caching is wanted)

但是当我不断更改应用程序的一些 JavaScript 页面时,浏览器使用旧版本的缓存(服务器上的缓存控制是正确的 - 但我没有需要过度缓存的正常用例)

So is there a way to tell the (default) Android Browser to permanently disable it's cache?
Or is it possible to use an adbcommand to clear the cache?

那么有没有办法告诉(默认)Android 浏览器永久禁用它的缓存?
或者是否可以使用adb命令清除缓存?

回答by jfriend00

For the duration of debugging, add a meta tag to your page to disable caching.

在调试期间,向页面添加元标记以禁用缓存。

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

http://www.i18nguy.com/markup/metatags.html

http://www.i18nguy.com/markup/metatags.html

回答by UdayaLakmal

Using adb command you can clear browser cache and user data

使用 adb 命令可以清除浏览器缓存和用户数据

adb shell pm clear com.android.browser

but this will not work if you issue this from the android program runtime

但是如果你从 android 程序运行时发出这个,这将不起作用

see my previous questionregarding that

看到我以前的问题就在于

Although that is temporary solution if you need to clear android browser cache continuously using background running service it can be done with "android.content.pm.IPackageDataObserver".if you looking for that following is that service It tested and work fine

尽管这是临时解决方案,如果您需要使用后台运行服务连续清除 android 浏览器缓存,它可以通过“android.content.pm.IPPackageDataObserver”来完成。如果您正在寻找以下服务,它经过测试并且工作正常

import java.util.List;

import android.app.PendingIntent;
import android.app.Service;    
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.Handler;
import android.os.IBinder;


public class CacheCleanerService extends Service {

public static final String REFRESH_INTENT="tritop.android.slwcachecleanerwidget.REFRESH";
public static final String CLEAR_INTENT="tritop.android.slwcachecleanerwidget.CLEAR";
public static final long RECOUNTNDELAY=1500;
private boolean mDND=false;
private Handler mHandler;
private int statsCounter;
private long mCacheSum;
private StatsObserver mStatsObs;
private ClearCacheObserver mClearObs;
private PackageManager mPM;
private List<PackageInfo> mInstPkg;


private Runnable mTriggerCount = new Runnable()
{

    public void run()
    {
     countCache();
    }
 };

 private Runnable mAutoKill = new Runnable()
    {

        public void run()
        {
         stopSelf();
        }
     };


//More info in ApplicationState.java @ android.git.kernel.org
class StatsObserver extends IPackageStatsObserver.Stub{
    public void onGetStatsCompleted(PackageStats stats,boolean bl){
        mCacheSum+=stats.cacheSize;
        statsCounter++;
        if(statsCounter>=mInstPkg.size()){
            updateWidgets();
        }
    }
}

class ClearCacheObserver extends IPackageDataObserver.Stub {
    public void onRemoveCompleted(final String packageName, final boolean succeeded) {
     }
 }

private void countCache() {
    statsCounter = 0;
    mCacheSum = 0;
    mInstPkg= mPM.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES |
               PackageManager.GET_DISABLED_COMPONENTS);
    for(PackageInfo pInfo: mInstPkg){
         //  mPM.getPackageSizeInfo(pInfo.packageName, mStatsObs);
    }
}

private void clearCache(){
    mInstPkg= mPM.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES |
               PackageManager.GET_DISABLED_COMPONENTS);
    //mPM.freeStorageAndNotify(Integer.MAX_VALUE, mClearObs);
    //mPM.freeStorageAndNotify(Long.MAX_VALUE, mClearObs);
    mHandler.postDelayed(mTriggerCount, RECOUNTNDELAY);
}



@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    mStatsObs = new StatsObserver();
    mClearObs = new ClearCacheObserver();
    mPM = getPackageManager();
    mHandler = new Handler();
}

@Override
public void onDestroy() {
    mHandler.removeCallbacks(mAutoKill);
    mHandler.removeCallbacks(mTriggerCount);
    mDND=false;
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    if(!mDND){
        mHandler.postDelayed(mAutoKill, 20000);
        mDND=true;
        mCacheSum=0;
        statsCounter=0;
        if(CLEAR_INTENT.equals(intent.getAction())){
            clearCache();
        }
        else{
            countCache();
        }
    }
}

}

}

回答by Pedantic

Try appending the time (in long, ms since epoch format) as a query parameter for each call. This will prevent the cached copy being used. We do this in our normal J2EE/Struts/Tomcat stack and it works well with all (desktop) browsers.

尝试附加时间(长格式,自纪元以来的毫秒格式)作为每次调用的查询参数。这将阻止使用缓存副本。我们在普通的 J2EE/Struts/Tomcat 堆栈中执行此操作,并且它适用于所有(桌面)浏览器。

If you want cached behavior in production its easy to remove the extra params.

如果您希望在生产中缓存行为,则可以轻松删除额外的参数。

回答by Alexandre de Champeaux

Why aren't you using the cache manifest?

为什么不使用缓存清单?

https://developer.mozilla.org/en/Offline_resources_in_Firefox

https://developer.mozilla.org/en/Offline_resources_in_Firefox

If you put your js files in the network section, this should do what you need :

如果您将 js 文件放在网络部分,这应该可以满足您的需求:

Files listed under the NETWORK: section header in the cache manifest file are white-listed resources that require a connection to the server. All requests to such resources bypass the cache, even if the user is offline. Wildcards may be used.

缓存清单文件中 NETWORK: 部分标题下列出的文件是需要连接到服务器的白名单资源。对此类资源的所有请求都会绕过缓存,即使用户处于离线状态。可以使用通配符。

回答by Russell

I am using an Android emulator and was able to clear the built in Browser by opening the Browser and clicking on menu. There I selected Settings and then "Privacy and security". This gave the option to click on clear Cache.

我正在使用 Android 模拟器,并且能够通过打开浏览器并单击菜单来清除内置浏览器。在那里我选择了设置,然后选择了“隐私和安全”。这提供了单击清除缓存的选项。

回答by Tank

I came across a similar problem using lots of AJAX and constantly refreshing the emulator browser. These two methods helped me.

我在使用大量 AJAX 并不断刷新模拟器浏览器时遇到了类似的问题。这两种方法对我有帮助。

AJAX - set the request header to get the reqeust if-modified-since.

AJAX - 设置请求头以获取 reqeust if-modified-since。

 xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");

Or with jquery $.ajax()

或者使用 jquery $.ajax()

beforeSend: function(xhr) {
    xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
}

If you happen to be using PHP - set the header information.

如果您碰巧使用 PHP - 设置标题信息。

header("Expires: Sat, 1 Jan 2005 00:00:00 GMT");
header("Last-Modified: ".gmdate( "D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

Source: http://ajaxpatterns.org/XMLHttpRequest_Call

来源:http: //ajaxpatterns.org/XMLHttpRequest_Call