清除android中其他应用程序的缓存不重复
嗨,我正在尝试从我的应用程序中清除其他应用程序缓存,我现在可以使用以下代码清除其他应用程序缓存,直到android 4.1.2版本清除android中其他应用程序的缓存不重复,android,caching,Android,Caching,嗨,我正在尝试从我的应用程序中清除其他应用程序缓存,我现在可以使用以下代码清除其他应用程序缓存,直到android 4.1.2版本 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.Activity; import android.content.pm.IPackageDataObserver; import android.content
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中运行相同的应用程序时,缓存并没有被清除。请有人帮帮我。我已经找到了解决方案,工作代码片段如下,只需将
您的所有\u缓存\u都属于\u我们
长值从1000000000L
更改为long.MAX\u值
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) {
}
});
}
}
}
您收到了什么错误或异常?感谢您的评论,没有错误或异常@Sunny,代码未清除缓存。这可能会有帮助,谢谢@Sunny,但我使用的是fae
freesetorage和notify
方法,而且我也遵循了您发布的链接,但是问题仍然存在。@ChethanShetty您能告诉我如何传递特定的应用程序包名称,以便只删除该包缓存吗?此方法在onRemoveCompleted@ChethanShetty你能帮我一下吗这段代码对我不起作用final Class[]classes={Long.TYPE,Class.forName(“android.content.pm.IPackageDataObserver”)};