2
我想開發一個可以擦除其他應用程序緩存數據的Android應用程序。我試圖瀏覽所有的博客,但沒有一個爲我工作。我可以通過下面的代碼
如何從另一個在棉花糖的Android應用程序清除其他應用程序緩存?
參考清除我的應用程序的緩存:
How to delete other applications cache from our android app?
private static final long CACHE_APP = Long.MAX_VALUE;
private CachePackageDataObserver mClearCacheObserver;
btnCache.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearCache();
}
});//End of btnCache Anonymous class
void clearCache()
{
if (mClearCacheObserver == null)
{
mClearCacheObserver=new CachePackageDataObserver();
}
PackageManager mPM=getPackageManager();
@SuppressWarnings("rawtypes")
final Class[] classes= { Long.TYPE, IPackageDataObserver.class };
Long localLong=Long.valueOf(CACHE_APP);
try
{
Method localMethod=
mPM.getClass().getMethod("freeStorageAndNotify", classes);
/*
* Start of inner try-catch block
*/
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();
}
/*
* End of inner try-catch block
*/
}
catch (NoSuchMethodException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}//End of clearCache() method
private class CachePackageDataObserver extends IPackageDataObserver.Stub
{
public void onRemoveCompleted(String packageName, boolean succeeded)
{
}//End of onRemoveCompleted() method
}//End of CachePackageDataObserver instance inner class
而且也在你的src文件夾中創建一個包名爲android.content.pm
該包內創建一個名爲IPackageDataObserver
.aidl的文件並粘貼以下代碼
package android.content.pm;
/**
* API for package data change related callbacks from the Package Manager.
* Some usage scenarios include deletion of cache directory, generate
* statistics related to code, data, cache usage(TODO)
* {@hide}
*/
oneway interface IPackageDataObserver {
void onRemoveCompleted(in String packageName, boolean succeeded);
}
和您的清單一定要使用下面的代碼
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
以上成功清理棒棒糖但是當這種代碼在執行Marshamallow,那麼它是不是清除所有數據代碼。爲什麼這樣做??????????????
任何建議將被接受。或另一種解決方法。謝謝。
我有同樣的問題。如果您發現任何解決方案,請與我分享 –