如何在Android中使用辅助功能服务删除缓存?

Sau*_*try 5 android caching

我正在研究缓存清理应用程序,在对谷歌进行研究后,我发现Android系统已将"CLEAR_APP_CACHE"权限移至"签名,特权"状态.所以我无法用freeStorageAndNotify方法清除缓存.

谷歌Playstore上的应用程序,如CCleaner,Power Clean等..正在使用Accessibility Service删除缓存.

我还为我的应用程序创建了基本的辅助功能服务,但不知道如何删除应用程序的缓存

Par*_*nos 0

您可以获得已安装应用程序的列表并删除缓存,例如:

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

它相当于设置 --> 应用程序管理器 --> 您的应用程序 --> 清除数据下的清除数据选项。(评论中)

为了使您的应用程序支持这一点,您还应该遵循谷歌的特权权限白名单