Wednesday, May 30, 2018

How To Delete Cache Using Accessibility Service in Android?

Leave a Comment

I'm working on cache cleaner app, after doing research on google I found that Android system has moved "CLEAR_APP_CACHE" permission to "signature, privileged" state. So I'm unable clear cache with freeStorageAndNotify method.

Apps on google playstore like CCleaner, Power Clean etc.. are using Accessibility Service To Delete Cache.

I have also created basic accessibility service for my app, but don't know how to delete cache of apps

1 Answers

Answers 1

You can get a list of installed apps and delete cache like:

    public static void clearALLCache()             {                 List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);             for (int i=0; i < packList.size(); i++)             {                 PackageInfo packInfo = packList.get(i);                 if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)                 {                     String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();                     try {                         // clearing app data     //                    Runtime runtime = Runtime.getRuntime();     //                    runtime.exec("pm clear "+packInfo.packageName);                         Context context = getApplicationContext().createPackageContext(packInfo.packageName,Context.CONTEXT_IGNORE_SECURITY);                         deleteCache(context);                      } catch (Exception e) {                         e.printStackTrace();                     }                 }             }             }  public static void deleteCache(Context context) {         try {             File dir = context.getCacheDir();             deleteDir(dir);         } catch (Exception e) {}     }      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();         } else if(dir!= null && dir.isFile()) {             return dir.delete();         } else {             return false;         }     } 

It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data. (In comment)

For making your application support this you should also follow Privileged Permission Whitelisting from google

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment