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"/>