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

发布于 2024-10-20 06:52:02 字数 245 浏览 9 评论 0原文

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

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

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

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.

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

层林尽染 2024-10-27 06:52:02

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

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

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

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

帅气尐潴 2024-10-27 06:52:02

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

adb shell pm clear com.android.browser

但如果您从 android 程序运行时发出此命令,则这将不起作用,

请参阅我之前的 问题 虽然

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

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();
        }
    }
}

}

Using adb command you can clear browser cache and user data

adb shell pm clear com.android.browser

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

see my previous question regarding 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

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();
        }
    }
}

}

梦里梦着梦中梦 2024-10-27 06:52:02

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

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

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.

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

请你别敷衍 2024-10-27 06:52:02

为什么不使用缓存清单?

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

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

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

Why aren't you using the cache manifest?

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 :

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.

念三年u 2024-10-27 06:52:02

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

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

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

或者使用 jquery $.ajax()

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

如果您碰巧使用 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");

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

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

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

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

Or with 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.

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

奈何桥上唱咆哮 2024-10-27 06:52:02

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文