Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get processor speed and RAM in android device

Tags:

android

Could anyone help me how to get processor name, speed and RAM of Android device via code.

like image 711
Klose Avatar asked Feb 20 '12 14:02

Klose


People also ask

How can I know my phone RAM and processor?

What to Know. Tap Settings > About Phone > RAM to view the amount of RAM your phone has. Tap Settings > About Phone > Build Version several times to activate Developer Options to view advanced RAM info.


2 Answers

You can get processor, RAM and other hardware related information as we normally get in Linux. From terminal we can issue these command in a normal Linux system. You don't need to have a rooted device for this.

$ cat /proc/cpuinfo 

Similarly you can issue these commands in android code and get the result.

public void getCpuInfo() {
    try {
        Process proc = Runtime.getRuntime().exec("cat /proc/cpuinfo");
        InputStream is = proc.getInputStream();
        TextView tv = (TextView)findViewById(R.id.tvcmd);
        tv.setText(getStringFromInputStream(is));
    } 
    catch (IOException e) {
        Log.e(TAG, "------ getCpuInfo " + e.getMessage());
    }
}

public void getMemoryInfo() {
    try {
        Process proc = Runtime.getRuntime().exec("cat /proc/meminfo");
        InputStream is = proc.getInputStream();
        TextView tv = (TextView)findViewById(R.id.tvcmd);
        tv.setText(getStringFromInputStream(is));
    } 
    catch (IOException e) {
        Log.e(TAG, "------ getMemoryInfo " + e.getMessage());
    }
}

private static String getStringFromInputStream(InputStream is) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;

    try {
        while((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
    } 
    catch (IOException e) {
        Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
    }
    finally {
        if(br != null) {
            try {
                br.close();
            } 
            catch (IOException e) {
                Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
            }
        }
    }       

    return sb.toString();
}
like image 199
Rukmal Dias Avatar answered Oct 14 '22 23:10

Rukmal Dias


its only possible on a rooted device or your app is running as system application.

For wanted informations you have to look in the running kernel, as i know this informations cant be obtained by the android system itself.

To obtain informations about the CPU, you can read and parse this file: /proc/cpuinfo

To obtain memory informations, you can read and parse this file: /proc/memory

like image 23
Andreas Avatar answered Oct 15 '22 00:10

Andreas