5

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

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • try this answer https://stackoverflow.com/questions/8474444/how-can-i-clear-the-android-app-cache – Paraskevas Ntsounos May 23 '18 at 11:20
  • [Android Accessibility Service](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#clearCache()) not sure if it’s useful. I just scrolling around the doc and see this and dramatically found this question lol ( I’m not Android developer and is trying to use Python Pyjnius to access the android class ) – Paul Lam Apr 14 '22 at 12:54

1 Answers1

0

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

Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
  • @Jyoti JK no, i test in my device is not rooted and works, need permissions in manifest – Paraskevas Ntsounos May 24 '18 at 07:32
  • How to clear cache only `Settings --> Application Manager --> Your App --> Clear cache` – Jyoti JK May 24 '18 at 09:31
  • @Jyoti JK check my updated answer, its only clear data off installed application not from system pre-installed – Paraskevas Ntsounos May 24 '18 at 10:15
  • Thanks for your effort. I tried this method already. I have done clearing cache with the help of **`IPackageStatsObserver.aidl`** . But it works api<23. But for api>23 , I didn't get any trick. Some of the apps takes accessibility permission to clean cache . That's why I started a bounty to know how they clean cache. – Jyoti JK May 24 '18 at 10:22