'Access a variable from the inner class

i am trying to access a variable that resides in my inner class i am posting as separate question because i am not able to understand in my current situation

public class MyActivity extends Activity {
  static double returnAppSize;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_app_breaker_layout);

    try {
        double appSize = getAppSize("com.supercell.clashofclans");
        System.out.println("appSize: " + appSize);

        double appSize3 = getAppSize("com.dotgears.flappybird");
        System.out.println("appSize: " + appSize3);

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

And following is the method from which i want to access the variable named "codeSize"

  public double getAppSize(String packageName) throws NoSuchMethodException,
        IllegalAccessException, IllegalArgumentException,
        InvocationTargetException {
      returnAppSize = 0.0;

    final String pkName = packageName; // "com.dotgears.flappybird";com.supercell.clashofclans

    PackageManager pm = getPackageManager();

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

    getPackageSizeInfo.invoke(pm, pkName, new IPackageStatsObserver.Stub() {
        private double appSize;

        @Override
        public void onGetStatsCompleted(PackageStats pStats,
                boolean succeeded) throws RemoteException {
            appSize = ((pStats.codeSize) / 1024) / 1024;
            returnAppSize = appSize;
            Log.i(pkName, "codeSize: " + returnAppSize);
        }
    });
      Log.i(pkName, "codeSize333: " + returnAppSize);
      return returnAppSize;
    }
}

The problems i am facing here are:

  1. the prints in the onCreate method print before the size is calculated, how to prevent this problem what is causing the size to be calculated after and why the control is being transferred earlier back to the call. (I think this is the main problem)

  2. how to get the value of "appSize" in the global varialbe? is the way i am doing right?



Solution 1:[1]

  1. What causes the print to happen before the calculation? The calculation takes time, so it is probably calculated on a different thread, and when it ends, it notify the IPackageStatsObserver.Stub that you created, and invokes the onGetStatsCompleted. While this happens in a different thread, your main thread continues and gets to the point of return returnAppSize; with the initial value of 0.0. This is not a problem, this is a normal behaviour.

  2. What you can do about it You can add to your activity method like:

    public void calcDone(int size){
        //printing is here
    }
    

    and call it from inside onGetStatsCompleted. But remember that if you want to show something in the ui use runOnUiThread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Uriel Frankel