如何清除另一个应用程序的缓存?
How to clear the cache of another application?
我正在尝试删除缓存,不是我的应用程序,但是当我查看设置时,一切都保持不变。据我了解,您需要找到应用程序包中的/cache文件夹并删除它,还有其他方法吗?
现在我得到安装在 phone:
上的应用程序列表
private fun getListOfAppsInfo(activity: Activity, isAll: Int): MutableList<ApplicationInfo> {
val appForReturnedList = mutableListOf<ApplicationInfo>()
val appsInfoList: MutableList<ApplicationInfo> = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
activity.packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
} else {
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val arrayAppsNew = mutableListOf<ApplicationInfo>()
val listAppsReturned = activity.packageManager.queryIntentActivities(intent, 0)
listAppsReturned.forEach {
arrayAppsNew.add(it.activityInfo.applicationInfo)
}
arrayAppsNew
}
val appsInstalled: MutableList<ApplicationInfo> = mutableListOf()
val appsSystem: MutableList<ApplicationInfo> = mutableListOf()
(appsInfoList.indices).forEach { i ->
// if (appsInfoList[i].packageName != activity.packageName) {
if (appsInfoList[i].flags and ApplicationInfo.FLAG_SYSTEM == 1) {
appsSystem.add(appsInfoList[i])
} else {
appsInstalled.add(appsInfoList[i])
getPackagePathByPackageName(this, appsInfoList[i].packageName)
}
// }
}
when (isAll) {
ALL_APPS -> {
appForReturnedList.addAll(appsInstalled)
appForReturnedList.addAll(appsSystem)
}
USER_APPS -> appForReturnedList.addAll(appsInstalled)
SYSTEM_APPS -> appForReturnedList.addAll(appsSystem)
else -> appForReturnedList.addAll(appsInstalled)
}
return appForReturnedList
}
private fun getAppsListInfo(activity: Activity, isAll: Int): ArrayList<String> {
val appsArrayList: ArrayList<String> = ArrayList()
for (app in getListOfAppsInfo(activity, isAll)) {
appsArrayList.add(app.packageName)
}
return appsArrayList
}
然后根据包名,找到我想要的文件夹的路径,如果有的话:
private fun getPackagePathByPackageName(activity: Activity, packageName: String): String {
val packageManager = activity.packageManager
var packagePath = packageName
val packageInfo = packageManager.getPackageInfo(packagePath, 0)
var appCacheDir = ""
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
if (dir.exists()) {
appCacheDir = dir.absolutePath
}
return appCacheDir
}
并删除它:
private fun deleteCache(context: Context, packagePathByPackageName: String) {
try {
// val dir: File = context.cacheDir
val dir = File(packagePathByPackageName)
deleteDir(dir)
} catch (e: Exception) {
}
}
private fun deleteDir(dir: File?): Boolean {
return if (dir != null && dir.isDirectory) {
dir.deleteRecursively()
val children: Array<String> = dir.list()
for (i in children.indices) {
val success = deleteDir(File(dir, children[i]))
if (!success) {
return false
}
}
dir.deleteRecursively()
} else if (dir != null && dir.isFile) {
dir.deleteRecursively()
} else {
false
}
}
但是没有任何反应,即使我将路径替换为我的应用程序。
这是权限列表:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_MANAGE_WRITE_SETTINGS " />
<uses-permission
android:name="android.permission.CLEAR_APP_CACHE"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.GET_PACKAGE_SIZE"
tools:ignore="ProtectedPermissions" />
<queries>
<package android:name="QUERY_ALL_PACKAGES" />
<package android:name="com.android.settings" />
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
<intent>
<action android:name="android.intent.action.POWER_USAGE_SUMMARY" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
</intent>
</queries>
并且我请求权限 READ_EXTERNAL_STORAGE、WRITE_EXTERNAL_STORAGE、ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,也许有些权限丢失了?或者这根本无法完成,似乎基于此 api 29+ 之后的 https://developer.android.com/training/data-storage/app-specific 您无法获得此类访问权限。我在 30+ phone 上测试过,带有缓存的文件夹真的不可见,在 21+ 上它们是可见的但不删除任何东西,在模拟器上是一样的。你能告诉我这是否可以完成,如果可以,我做错了什么?
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
您没有其他应用程序对此位置的读取或写入权限。自 Android 1.0 以来一直如此,该版本发布于将近 14 年前的此时。
我正在尝试删除缓存,不是我的应用程序,但是当我查看设置时,一切都保持不变。据我了解,您需要找到应用程序包中的/cache文件夹并删除它,还有其他方法吗? 现在我得到安装在 phone:
上的应用程序列表 private fun getListOfAppsInfo(activity: Activity, isAll: Int): MutableList<ApplicationInfo> {
val appForReturnedList = mutableListOf<ApplicationInfo>()
val appsInfoList: MutableList<ApplicationInfo> = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
activity.packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
} else {
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val arrayAppsNew = mutableListOf<ApplicationInfo>()
val listAppsReturned = activity.packageManager.queryIntentActivities(intent, 0)
listAppsReturned.forEach {
arrayAppsNew.add(it.activityInfo.applicationInfo)
}
arrayAppsNew
}
val appsInstalled: MutableList<ApplicationInfo> = mutableListOf()
val appsSystem: MutableList<ApplicationInfo> = mutableListOf()
(appsInfoList.indices).forEach { i ->
// if (appsInfoList[i].packageName != activity.packageName) {
if (appsInfoList[i].flags and ApplicationInfo.FLAG_SYSTEM == 1) {
appsSystem.add(appsInfoList[i])
} else {
appsInstalled.add(appsInfoList[i])
getPackagePathByPackageName(this, appsInfoList[i].packageName)
}
// }
}
when (isAll) {
ALL_APPS -> {
appForReturnedList.addAll(appsInstalled)
appForReturnedList.addAll(appsSystem)
}
USER_APPS -> appForReturnedList.addAll(appsInstalled)
SYSTEM_APPS -> appForReturnedList.addAll(appsSystem)
else -> appForReturnedList.addAll(appsInstalled)
}
return appForReturnedList
}
private fun getAppsListInfo(activity: Activity, isAll: Int): ArrayList<String> {
val appsArrayList: ArrayList<String> = ArrayList()
for (app in getListOfAppsInfo(activity, isAll)) {
appsArrayList.add(app.packageName)
}
return appsArrayList
}
然后根据包名,找到我想要的文件夹的路径,如果有的话:
private fun getPackagePathByPackageName(activity: Activity, packageName: String): String {
val packageManager = activity.packageManager
var packagePath = packageName
val packageInfo = packageManager.getPackageInfo(packagePath, 0)
var appCacheDir = ""
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
if (dir.exists()) {
appCacheDir = dir.absolutePath
}
return appCacheDir
}
并删除它:
private fun deleteCache(context: Context, packagePathByPackageName: String) {
try {
// val dir: File = context.cacheDir
val dir = File(packagePathByPackageName)
deleteDir(dir)
} catch (e: Exception) {
}
}
private fun deleteDir(dir: File?): Boolean {
return if (dir != null && dir.isDirectory) {
dir.deleteRecursively()
val children: Array<String> = dir.list()
for (i in children.indices) {
val success = deleteDir(File(dir, children[i]))
if (!success) {
return false
}
}
dir.deleteRecursively()
} else if (dir != null && dir.isFile) {
dir.deleteRecursively()
} else {
false
}
}
但是没有任何反应,即使我将路径替换为我的应用程序。 这是权限列表:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_MANAGE_WRITE_SETTINGS " />
<uses-permission
android:name="android.permission.CLEAR_APP_CACHE"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.GET_PACKAGE_SIZE"
tools:ignore="ProtectedPermissions" />
<queries>
<package android:name="QUERY_ALL_PACKAGES" />
<package android:name="com.android.settings" />
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
<intent>
<action android:name="android.intent.action.POWER_USAGE_SUMMARY" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
</intent>
</queries>
并且我请求权限 READ_EXTERNAL_STORAGE、WRITE_EXTERNAL_STORAGE、ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,也许有些权限丢失了?或者这根本无法完成,似乎基于此 api 29+ 之后的 https://developer.android.com/training/data-storage/app-specific 您无法获得此类访问权限。我在 30+ phone 上测试过,带有缓存的文件夹真的不可见,在 21+ 上它们是可见的但不删除任何东西,在模拟器上是一样的。你能告诉我这是否可以完成,如果可以,我做错了什么?
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
您没有其他应用程序对此位置的读取或写入权限。自 Android 1.0 以来一直如此,该版本发布于将近 14 年前的此时。