在android中清除其他应用程序的缓存不重复

时间:2013-09-21 06:42:33

标签: android caching

您好我正在尝试从我的应用程序中清除其他应用程序缓存,我现在可以使用以下代码清除其他应用程序缓存,直到4.1.2版本的android

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

IPackageDataObserver代码为

package android.content.pm;

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

但是当我在4.2及以上的android版本中运行相同的应用程序时,缓存不会清除。请有人帮助我。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案人员,工作代码段如下,只需将ALL_YOUR_CACHE_ARE_BELONG_TO_US长值从1000000000L更改为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) {

            }
          });
        }
        }
        }