我想在服务器上执行一些活动后清除我的应用程序的缓存。它在三星手机(Android 5.0)中运行良好,但在华为手机(Android 7)中没有清除缓存。我认为 Nougat 没有授予该应用程序的权限。任何有关此问题的帮助将不胜感激。
缓存清除代码:
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();
}
在清单中添加权限:
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"
tools:ignore="ProtectedPermissions"/>
慕森王
慕森卡
相关分类