我正在嘗試洗掉快取,而不是我的應用程式,但是當我查看設定時,一切都保持不變。據我了解,需要在應用程式包中找到/cache檔案夾并洗掉,還有其他方法嗎?現在我得到了手機上安裝的應用程式串列:
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,也許有些缺失?或者這根本無法完成,似乎基于此https://developer.android.com/training/data-storage/app-specific在 api 29 之后您無法獲得此類訪問權限。我在 30 多部手機上對其進行了測驗,帶有快取的檔案夾實際上不可見,在 21 多部時它們可見但不洗掉任何內容,在模擬器上也是如此。你能告訴我這是否可以做到,如果可以,我做錯了什么?
uj5u.com熱心網友回復:
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
對于其他應用程式,您既沒有對該位置的讀取也沒有寫入權限。自 14 年前發布的 Android 1.0 以來,情況一直如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483095.html
上一篇:創建后使用資料庫物件
