[android] get installed app size

 

private void getTotalSize()
{
   PackageManager packageManager = getApplicationContext().getPackageManager();

   List<PackageInfo> packs = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);

   for (int i = 0; i < packs.size(); i++)
   {
      final PackageInfo p = packs.get(i);
      Method getPackageSizeInfo;
      try
      {
         getPackageSizeInfo = packageManager.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
         getPackageSizeInfo.invoke(packageManager, p.packageName, new IPackageStatsObserver.Stub()
         {
            @Override
            public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException
            {
               if (pStats.codeSize > 0)
                  Log.w("MainActivity.java | onGetStatsCompleted", "|" + p.packageName + "|" + pStats.codeSize);
            }
         });
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

 

안드로이드 SDK 소스에서 PackageManager, IPackageStatsObserver, UserHandle 을 복사해와야 한다.
그리고 오류를 고쳐줘야 하는데,

 

public static final int myUserId()
{
   return getUserId(android.os.Process.myUid());
}

 

public void getPackageSizeInfo(String packageName, IPackageStatsObserver observer)
{
   getPackageSizeInfo(packageName, android.content.pm.UserHandle.myUserId(), observer);
}

 

이거 말고는 다른 건 다 주석처리해도 된다.

 

<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>