jeudi 28 janvier 2016

How to call the hidden Android method getTotalUss() using Java reflection?

I am using Android Studio 1.5.1 targeting Android API 18 (before Android KitKat 4.4, so I’m dealing with Dalvik, not ART runtime).

My questions are:

  1. How to call the hidden Android method getTotalUss() using Java reflection?
  2. If not possible, how to find the current process USS (Unique Set Size) memory programmatically?

I am trying to use the code below to do that but I am getting a compiler error "Unexpected token" at the statement labelled //ERROR! below in the code.

    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);

    List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();

    Map<Integer, String> pidMap = new TreeMap<Integer, String>();
    for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
    {
        pidMap.put(runningAppProcessInfo.pid, runningAppProcessInfo.processName);
    }

    Collection<Integer> keys = pidMap.keySet();
    int id= android.os.Process.myPid();
    for(int key : keys)
    {
        if (key != id) continue;
        int pids[] = new int[1];
        int Uss;
        pids[0] = key;
        android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
        for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)
        {
            try {

                Class c;
                c = Class.forName("android.app.ActivityManager");
                Method m = c.getMethod("getTotalUss", null);
                Uss = m.invoke(null,int); //             << == ERROR!
            } catch (ClassNotFoundException e) {
            }
            catch (NoSuchMethodException e) {
            }
            catch (IllegalAccessException e) {
            }

            System.out.println("** Uss =  " +  Uss);





Aucun commentaire:

Enregistrer un commentaire