MemoryInfo

...now browsing by tag

 
 

Get the ActivityManager for MemoryInfo

Sonntag, Februar 22nd, 2009

Attention: My android posts will now be published on droidnova.com

During my first steps of Android programming, i want to know how much memory the device has or how much is really used.
In the Android API I found the ActivityManager class which has a inner class MemoryInfo. Simply instantiation of the ActivityManager failed because there is no visible constructor.
Searching the web gave no answers only open questions from other developer. So I looked further and I found a method of the Activity class called getSystemService(String name). With a constant from Context.ACTIVITY_SERVICE as parameter it returns me an Object I can cast to ActivityManager. With an instance from MemoryInfo I could now get the information I wanted.
At the moment I don’t know how to display the memory information in the UI, but I just started :D
Here is my short code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package de.warrenfaith.android.memory;
 
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Bundle;
 
public class MemoryTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager mgr = (ActivityManager) super.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        mgr.getMemoryInfo(memInfo);
        setContentView(R.layout.main);
    }
}
Stoppt die Vorratsdatenspeicherung! Jetzt klicken & handeln!Willst du auch bei der Aktion teilnehmen? Hier findest du alle relevanten Infos und Materialien: