我正在尝试开发一个可以删除其他应用程序缓存数据的
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; } }
我想清除其他应用程序的缓存,任何人都可以帮助我,如果我错了请纠正我,提前谢谢.
解决方法
API 23中不再支持此API,即Marshmallow.在Marshmallow中不允许使用权限.
但是通过询问附件的运行时许可还有另一种方法.从Play商店试试应用All-in-one Toolbox.此应用程序即使在Marshmallow中也能清除其他应用程序缓存.那么我们应该可以这样做.
我正在研究这个问题.一旦找到解决方案,我将更新答案.谢谢.
OLD ANSWER IS AS FOLLOWS
我使用了以下代码,现在我可以清除所有应用程序的缓存而无需生根,它对我来说非常合适,
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"/>
如果你们发现任何问题请随时与我联系,谢谢.