【发布时间】:2013-06-23 05:13:05
【问题描述】:
我正在尝试开发一个可以删除其他应用程序缓存数据的 android 应用程序,我尝试浏览所有博客,但没有一个对我有用,我可以通过以下代码清除我的应用程序缓存
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();
}
我的清单代码
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
我在 2.2、2.3 和 4.0 上测试了代码
在看到以下链接Android: Clear Cache of All Apps?的帖子后
我将代码更改为
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;
}
}
我想清除其他应用程序的缓存,请任何人帮助我,如果我错了,请纠正我,提前谢谢。
【问题讨论】:
-
你认为你会被允许处理其他应用程序的数据吗?
-
不是没有root,但如果你看到Play商店中可用的Clean Master,History Eraser等应用程序,他们正在完成任务。
-
这对我不起作用@David Wasser
-
请解释“不工作”。您是否在 logcat 中查找任何错误或其他消息(不要过滤 logcat,否则您可能会错过重要的东西)?您是否提供了正确的权限?发布您的清单。你在什么设备上测试过这个?