title: Android method invoke synchronously
body: <p>I'm getting installed application's size with this function:</p>
  <pre><code>Method getPackageSizeInfo = pm.getClass().getMethod(&quot;getPackageSizeInfo&quot;, String.class, IPackageStatsObserver.class);
  getPackageSizeInfo.invoke(pm, applicationInfo.packageName, new IPackageStatsObserver.Stub() {
    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
      //my code here
    }
  });
  </code></pre>
  <p>Since I have to display all informations in a <code>list</code>, how can I wait for this method to be completed before doing the same work for all my other applications?</p>
  <p>Is there a way to call this <code>Method.invoke</code> synchronously?</p>
  <h1>Edit</h1>
  <p>this is my full code:</p>
  <pre><code>    List&lt;ApplicationInfo&gt; applicationInfos;
      final PackageManager pm = ctx.getPackageManager();
      applicationInfos = pm.getInstalledApplications(0);
      Context ctx = getContext();
  
      for (ApplicationInfo ai :
              applicationInfos) {
          if ((ai.flags &amp; ApplicationInfo.FLAG_SYSTEM) == 0) {
              allApps.add(new MyApp(pm, ai, ctx));
          }
      }
  </code></pre>
  <p>and this is the constructor (obv I can separate the size retrieving part and put it outside the constructor.</p>
  <pre><code>public MyApp(PackageManager pm, ApplicationInfo applicationInfo, Context ctx){
  
      MyApp.this.packageName = applicationInfo.packageName;
      MyApp.this.label = applicationInfo.loadLabel(pm).toString();
      MyApp.this.icon = applicationInfo.loadIcon(pm);
  
  
      try {
          long installed = ctx
                  .getPackageManager()
                  .getPackageInfo(applicationInfo.packageName, 0)
                  .firstInstallTime;
  
          MyApp.this.installDate = new Date(installed);
      } catch (PackageManager.NameNotFoundException e) {
          e.printStackTrace();
          MyApp.this.installDate = null;
      }
  
      try {
          Method getPackageSizeInfo = pm.getClass().getMethod(
                  &quot;getPackageSizeInfo&quot;, String.class, IPackageStatsObserver.class);
          getPackageSizeInfo.invoke(pm, applicationInfo.packageName,
                  new IPackageStatsObserver.Stub() {
                      @Override
                      public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                              throws RemoteException {
  
                          int tempSize = Math.round((pStats.codeSize + pStats.cacheSize + pStats.dataSize) / 10000);
                          double finalSize = tempSize/100;
                          MyApp.this.size = finalSize;
                      }
                  });
  
      } catch (Exception ignored) {
  
      }
  
  }
  </code></pre>