Search code examples
androidactivity-manager

How i can refresh the value of memInf.availMem?


I don' t understand why the value of memInf.availMem is always the same after the first read. For example: when I start my app the value is 0.441 GB but after many refresh of timer it is always the same.

How i can refresh the value? It is impossible that the use of ram is always the same in several minutes.

This is my actually code to refresh the value (but it don't work):

int startUp = 2000;     // millisecondi di attesa per fare partire il timer
    int periodo = 1000;     // millisecondi di attesa per la ripetizione del timer

    Timer timer = new Timer();      //timer

    //il metodo run viene eseguito ad ogni scadenza del timer
    timer.scheduleAtFixedRate(
            new TimerTask() {
                String c;
                public void run() {
                    c = String.valueOf(roundTreDecimali(memInfo.availMem / 1073741824.0)) + " GB";
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            freeram.setText(c);
                            System.out.println(c);
                        }
                    });
                }
            },
            startUp, periodo
    );

P.S: The timer work because the Sistem.out.println(c) write in my logcat the value every second.

EDIT: i have adding a part of my code

actManager = (ActivityManager) getActivity().getSystemService(getActivity().ACTIVITY_SERVICE);
    memInfo = new ActivityManager.MemoryInfo();
    actManager.getMemoryInfo(memInfo);

Solution

  • Assuming that memInf is an instance of ActivityManager.MemoryInfo, you would need to call getMemoryInfo() on ActivityManager to update memInf on each run() call.