简体   繁体   English

如何在 Android 中使用 Accessibility Service 删除缓存?

[英]How To Delete Cache Using Accessibility Service in Android?

I'm working on cache cleaner app, after doing research on google I found that Android system has moved "CLEAR_APP_CACHE" permission to "signature, privileged" state.我正在开发缓存清理应用程序,在对谷歌进行研究后,我发现 Android 系统已将“CLEAR_APP_CACHE”权限移至“签名、特权”状态。 So I'm unable clear cache with freeStorageAndNotify method.所以我无法使用freeStorageAndNotify方法清除缓存。

Apps on google playstore like CCleaner , Power Clean etc.. are using Accessibility Service To Delete Cache.谷歌 Playstore 上的应用程序,如CCleanerPower Clean等,正在使用辅助功能服务来删除缓存。

I have also created basic accessibility service for my app, but don't know how to delete cache of apps我还为我的应用创建了基本的无障碍服务,但不知道如何删除应用缓存

You can get a list of installed apps and delete cache like:您可以获得已安装应用程序的列表并删除缓存,例如:

    public static void clearALLCache()
            {
                List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
            for (int i=0; i < packList.size(); i++)
            {
                PackageInfo packInfo = packList.get(i);
                if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
                {
                    String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                    try {
                        // clearing app data
    //                    Runtime runtime = Runtime.getRuntime();
    //                    runtime.exec("pm clear "+packInfo.packageName);
                        Context context = getApplicationContext().createPackageContext(packInfo.packageName,Context.CONTEXT_IGNORE_SECURITY);
                        deleteCache(context);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            }

public static void deleteCache(Context context) {
        try {
            File dir = context.getCacheDir();
            deleteDir(dir);
        } catch (Exception e) {}
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        } else if(dir!= null && dir.isFile()) {
            return dir.delete();
        } else {
            return false;
        }
    }

It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data.相当于设置-->应用管理器-->你的应用-->清除数据下的清除数据选项。 (In comment) (在评论中)

For making your application support this you should also follow Privileged Permission Whitelisting from google为了使您的应用程序支持这一点,您还应该遵循 谷歌的特权权限白名单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM