如何从我们的Android应用程序,删除其他应用程序缓存? [英] How to delete other applications cache from our android app?
问题描述
我想开发一个Android应用程序,可以消除其他应用程序缓存的数据,我想浏览所有的博客,但他们没有工作对我来说,我可以能够清楚我的应用程序的缓存由以下code
I'm trying to develop an android app that could erase others application cache data, I tried to browse through all blogs but none of them worked for me, I can able to clear my application's cache by the following code
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists())
{
String[] children = appDir.list();
for (String s : children)
{
if (!s.equals("lib"))
{
deleteDir(new File(appDir, s));
Toast.makeText(DroidCleaner.this, "Cache Cleaned", Toast.LENGTH_LONG).show();
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
}
}
return dir.delete();
}
我的清单code
My manifest code
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
我测试了code在2.2,2.3和4.0
I tested the code on 2.2, 2.3 and 4.0
和看到帖子在以下链接的Android后:所有的应用程序清除缓存?
and after seeing the post in the following link Android: Clear Cache of All Apps?
我改变了我的code到
I changed my code to
PackageManager pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorage")) {
// Found the method I want to use
try {
long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
m.invoke(pm, desiredFreeStorage , null);
} catch (Exception e) {
// Method invocation failed. Could be a permission problem
}
break;
}
}
我要清除其他应用程序的缓存,任何机构可以请帮助我,请纠正我,如果我错了,在此先感谢。
I want to clear other application's cache, can any body please help me, please correct me if I'm wrong, Thanks in advance.
推荐答案
我用下面的code,现在我可以清除所有程序的缓存,而不生根,它的工作完美的我,
I used the following code and now I'm able to clear all application's cache without rooting, it's working perfectly for me,
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
的包内创建一个pacakge在名称的文件 IPackageDataObserver.aidl
并粘贴以下code将它
And also create a pacakge in your src
folder with the name android.content.pm
inside that package create a file in the name IPackageDataObserver.aidl
and paste the following code to it
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);
}
在你的清单请确保您使用以下code
and in your manifest make sure you used the following code
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
如果你们发现有什么问题随时联系我,谢谢。
If you guys find any problem feel free to contact me, Thanks.
这篇关于如何从我们的Android应用程序,删除其他应用程序缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!