software - android wikipedia

Delete system cache on android 6.0 (2)

I'm using method freeStorageAndNotify() with permission android.permission.CLEAR_APP_CACHE to delete system cache of all installed applications. But the method started throwing InvocationTargetException from android marshmallow 6.0 version. After googling the issues I found the same issue is reported here: Android M reflection method freeStorageAndNotify exception So here the conclusion was, freeStorageAndNotify() stopped working since google has raised the method's signature level now to signature|system.

But now the question is how other third party apps like 'Clean master' are still able to delete system cache of all installed applications by taking accessibility permission from user for 6.0 devices?

I don't think that 'Clean master' actually uses Accessibility Permissions to clean installed apps cache. But, if you're interested, this goal can be achieved by using AccessibilityService in your application. Within your class that extends AccessibilityService you have this callback:

    public void onAccessibilityEvent(AccessibilityEvent aEvent) {
       AccessibilityNodeInfo rootNode = aEvent.getSource();

Here you can invoke rootNode.findAccessibilityNodeInfosByViewId() or rootNode.findAccessibilityNodeInfosByText(), it will return all matching AccessibilityNodeInfo objects (sub-nodes) in tree. Then, you just need to detect which of them is Button (node.getClassName()) and call subNode.performAction(AccessibilityNodeInfo.ACTION_CLICK).

On Android M, you first need to to open system's App Info screen (you can find instructions here How can I start android application info screen programmatically?) for the concrete app and, by the scheme described above, perform sequential clicks on the buttons "Storage" —> "Clear cache".

In order to clear cache for all installed apps you probably have to iterate through the all installed apps (List<ApplicationInfo> installedApplications = context.getPackageManager.getInstalledApplications(0);) and repeat the procedure mentioned above.