简体   繁体   中英

Programmatically find Android system info

I am trying to programatically find the system info for Android devices, specifically:

  • RAM
  • CPU speed
  • # cores, architecture, etc.

Are there any Android classes that specify this information. I have been using the android.board library, but it doesn't seem to have everything that I want.

Let me tell you what I did, So others who visit this thread can come to know the steps:

1) parse /proc/meminfo command. You can find reference code here: Get Memory Usage in Android 2) use below code and get current RAM:

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;

Note: please note that - we need to calculate total memory only once. so call point 1 only once in your code and then after, you can call code of point 2 repetitively.

You can get most of the information you want from the /proc/cpuinfo file. Here is a tutorial on how to load and parse that file: http://www.roman10.net/how-to-get-cpu-information-on-android/

Additionally the information about the RAM can be obtained from the /proc/meminfo file

Here is the code snippet to get the current device RAM size.

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;

Agarwal's answer was very helpful. Had to modify it a bit since I'm not calculating free memory in an activity, but passing in an application context to a system utilities file:

From the main activity:

public class MyActivity extends Activity {
    ...
    public void onCreate(Bundle savedInstanceState) {
        ...
        MySystemUtils systemUtils = new MySystemUtils(this); // initializations
        ...
     }
}

In the SystemUtils file:

   MySystemUtils (Context appContext) { // called once from within onCreate
        MemoryInfo mi = new MemoryInfo();
        ActivityManager activityManager = ActivityManager)appContext.getSystemService(Activity.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1048576L;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM