Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1528
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T19:03:17+00:00 2022-05-30T19:03:17+00:00

android – How to clear the cache of another application?

  • 0

[ad_1]

I’m trying to delete the cache, not my application, but when I look in the settings, everything remains the same. As far as I understand, you need to find the /cache folder in the application package and delete it, are there any other ways?
Now I get a list of applications installed on the 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
    }

Then, by the package name, I find the path to the desired folder, if there is one:

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
    }

And delete it:

  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
        }
    }

But nothing happens, even if I substitute the path to my application.
Here is the list of permissions:

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

And I request the permissions READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, maybe some are missing? Or this cannot be done at all, it seems like based on this https://developer.android.com/training/data-storage/app-specific after api 29+ you cannot get such access. I tested it on a 30+ phone and the folders with the cache are really not visible, on 21+ they are visible but do not delete anything, it’s the same on emulators. Can you please tell me if this can be done at all, if so, what am I doing wrong?

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4773 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.