简体   繁体   中英

Get Cache Directory for all installed apps in android

I want to get exact size of cache directory of android installed apps. I have tried this.

List<ApplicationInfo> listOfApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
     for(ApplicationInfo applicationInfo : listOfApps) {

         try {

             Context context = createPackageContext(applicationInfo.packageName,CONTEXT_IGNORE_SECURITY);
             File file = context.getCacheDir();
             if(file!=null){
             String path = file.getAbsolutePath();

             long dir = file.length();
             Log.d("All_Package", applicationInfo.packageName + "\t\t" + MemoryFreedPredication.formatFileSize(String.valueOf(dir)) + " \t\t"+path);}
         } catch (PackageManager.NameNotFoundException e) {
             e.printStackTrace();
         }

Method to to format size is

public static String formatFileSize(String fileSize) {

    String[] sizes = {"B", "KB", "MB", "GB"};
    double len = Double.parseDouble(fileSize);
    int order = 0;

    if (len < 1024) {
        return String.format("%s %s", 1, sizes[1]);
    } else {
        while (len >= 1024 && order + 1 < sizes.length) {
            order++;
            len = len / 1024;
        }
    }

    len = Math.round(len * 100.0) / 100.0;
    return String.format("%s %s", len, sizes[order]);
}

But it is always returns 4Kb

I used aidl for this purpose.

final PackageManager packageManager = getPackageManager();

    final List<ApplicationInfo> applicationInfos = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);


    for(final ApplicationInfo runningTaskInfo : applicationInfos){

        try {
            Context context = createPackageContext(runningTaskInfo.packageName,CONTEXT_IGNORE_SECURITY);
           // PackageManager packageManager = context.getPackageManager();

            Method getPackageSizeInfo = packageManager.getClass().getMethod(
                    "getPackageSizeInfo", String.class, IPackageStatsObserver.class);

            getPackageSizeInfo.invoke(packageManager, runningTaskInfo.processName,
                    new IPackageStatsObserver.Stub() {

                        @Override
                        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                                throws RemoteException {

                            //Log.d("App_Size123","\n"+runningTaskInfo.loadLabel(packageManager)+"\t"+formatFileSize(String.valueOf(pStats.codeSize)));
                            Log.d("App_Size123","\n"+runningTaskInfo.loadLabel(packageManager)+"\t"+formatFileSize(String.valueOf(pStats.dataSize)));
                            //Log.d("App_Size123","\n"+runningTaskInfo.loadLabel(packageManager)+"\t"+formatFileSize(String.valueOf(pStats.cacheSize)));
                           //Log.d("App_Size123","\n"+runningTaskInfo.loadLabel(packageManager)+"\t"+formatFileSize(String.valueOf(pStats.externalDataSize)+"\n"));
                        }
                    });

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
  }

I have created 3 aidl files which are as follows.

PackageStats.aidl

interface IPackageStatsObserver {
void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
}

IPackageDataObserver.aidl

interface IPackageDataObserver {
void onRemoveCompleted(in String packageName, boolean succeeded);
}

PackageStats.aidl

parcelable PackageStats;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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