最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - How to Clear App Cache in Android (Like in App Info) Without Deleting Data? - Stack Overflow

programmeradmin7浏览0评论

I want to clear my Android app's cache programmatically, similar to how we do it from Settings > Apps > App Info > Storage & Cache > Clear Cache, but without deleting any app data. I've tried using the following function:

fun cleanCache(context: Context) {
    try {
        context.cacheDir.deleteRecursively()
        context.externalCacheDir?.deleteRecursively()
        context.toast("Cache cleared successfully")
    } catch (ignore: Exception) {
        context.toast("Failed to clear Cache")
    }
}

Additionally, I have cleared: WebView cache, Image loader memory cache, External caches.

Even after running this function, the cache is cleared but not entirely like when clearing it manually from system settings. How can I clear only the app cache (without affecting app data) exactly like the system "Clear Cache" button in App Info?

Questions: Is there a proper way to programmatically clear all app cache, similar to the system’s "Clear Cache" button? Are there any system permissions or APIs required to ensure complete cache clearing?

Any guidance or alternative approaches would be appreciated!

发布评论

评论列表(0)

  1. 暂无评论