0

Hi I'm trying to clean other applications cache from my application, I'm able to clear other apps cache now till 4.1.2 version of android using the following code

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.pm.IPackageDataObserver;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;

public class CacheNukerActivity extends Activity {
private static final long ALL_YOUR_CACHE_ARE_BELONG_TO_US=1000000000L;
private CachePackageDataObserver mClearCacheObserver;
private TextView tv=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.status);
tv.setText("Clearing cache...");

clearCache();
}

 private final void clearCache() {
if (mClearCacheObserver == null) {
  mClearCacheObserver=new CachePackageDataObserver();
}

PackageManager mPM=getPackageManager();

@SuppressWarnings("rawtypes")
final Class[] classes= { Long.TYPE, IPackageDataObserver.class };

Long localLong=Long.valueOf(ALL_YOUR_CACHE_ARE_BELONG_TO_US);
try {
  Method localMethod=
      mPM.getClass().getMethod("freeStorageAndNotify", classes);

  try {
    localMethod.invoke(mPM, localLong, mClearCacheObserver);
  }
  catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}
catch (NoSuchMethodException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}
}

private class CachePackageDataObserver extends
  IPackageDataObserver.Stub {
public void onRemoveCompleted(String packageName, boolean succeeded) {
  tv.post(new Runnable() {
    public void run() {
      tv.append(" cache cleared");
    }
  });
}
}
}

and the IPackageDataObserver code is

package android.content.pm;

oneway interface IPackageDataObserver {
    void onRemoveCompleted(in String packageName, boolean succeeded);
}

But when I run the same application in 4.2 and above android version, cache is not clearing. Please someone help me.

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45

1 Answers1

1

I've found the solution guys, the working code snippet is as follows, just change the ALL_YOUR_CACHE_ARE_BELONG_TO_US long value from 1000000000L to Long.MAX_VALUE

public class CacheNukerActivity extends Activity {
    private static final long ALL_YOUR_CACHE_ARE_BELONG_TO_US=Long.MAX_VALUE;
    private CachePackageDataObserver mClearCacheObserver;
    private TextView tv=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv=(TextView)findViewById(R.id.status);
        tv.setText("Clearing cache...");

        clearCache();
    }

         private final void clearCache() {
        if (mClearCacheObserver == null) {
          mClearCacheObserver=new CachePackageDataObserver();
        }

        PackageManager mPM=getPackageManager();

        @SuppressWarnings("rawtypes")
        final Class[] classes= { Long.TYPE, IPackageDataObserver.class };

        Long localLong=Long.valueOf(ALL_YOUR_CACHE_ARE_BELONG_TO_US);
        try {
          Method localMethod=
              mPM.getClass().getMethod("freeStorageAndNotify", classes);

          try {
            localMethod.invoke(mPM, localLong, mClearCacheObserver);
          }
          catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
        catch (NoSuchMethodException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
        }

        private class CachePackageDataObserver extends
          IPackageDataObserver.Stub {
        public void onRemoveCompleted(String packageName, boolean succeeded) {

            }
          });
        }
        }
        }
Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45