Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the Android browser cache permanently?

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?

like image 339
Chris Avatar asked Mar 06 '11 13:03

Chris


People also ask

Is browser cache permanent?

You could even add Cache-Control: no-cache so it won't be cached permanently by the browser or Cache-Control: no-store so it can't even be stored in temporary storage by the browser. Though, if you don't want your redirect to be permanent, it may be a better option to use a 302 or 307 redirect.

Is clearing cache permanent?

Although, constantly clearing your cache isn't a permanent solution since you'll eventually be reopening apps and revisiting websites at some point. The data will be re-cached, and the cycle will continue. If you're that strapped for memory, consider deleting old text messages, images, or video files on your device.


2 Answers

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

like image 51
jfriend00 Avatar answered Sep 19 '22 09:09

jfriend00


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

}

like image 42
UdayaLakmal Avatar answered Sep 19 '22 09:09

UdayaLakmal