0

I want to clear the cache of my app after performing some activity on server. Its working fine in Samsung phone (Android 5.0) But in Huawei phone (Android 7) is not clearing the cache. I think Nougat is not granting permission to the app. Any help regarding to this issue will be appreciated.

Cache clear code:

       public static void trimCache(Context context) {
         try {
        Toast.makeText(context,"hit on trim function 
        ",Toast.LENGTH_SHORT).show();
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            boolean check =deleteDir(dir);
            System.out.println("cache content check" +check);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
}

public static boolean deleteDir(File dir) {
   Log.e("Mainactivity","Inside delete");
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        Log.e("Mainactivity","Inside delete if condition"+children);
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}

Added permission in Manifest:

  <uses-permission android:name="android.permission.CLEAR_APP_CACHE"  
  tools:ignore="ProtectedPermissions"/>
immad Naseem
  • 3
  • 1
  • 5
  • try it : [enter link description here](https://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically) – Milad Mohamadi May 23 '19 at 21:07

2 Answers2

0

I think your problem is well explained in @CommonsWare answer here:

Prior to Android 6.0, CLEAR_APP_CACHE had a protectionLevel of dangerous, so ordinary SDK apps could request it in the manifest.

As of Android 6.0, CLEAR_APP_CACHE has a protectionLevel of signature|privileged. Ordinary Android apps cannot hold this permission. You can only hold this permission if your app is signed with the firmware's signing key or you are installed on the privileged system partition.

Legion
  • 760
  • 6
  • 23
0

For both, below Android 6.0 and above Android 6.0, this worked for me:

  1. Declaring Manifest permission:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  2. Declare this method from where you want to clear cache:

    private static void deleteCache(Context context) {
        try {
            File dir = context.getCacheDir();
            deleteDir(dir);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (String aChildren : children) {
                boolean success = deleteDir(new File(dir, aChildren));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        } else if (dir != null && dir.isFile()) {
            return dir.delete();
        } else {
            return false;
        }
    }
    
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50