Android Series: Display device CPU and Memory info

Android Series: Display device CPU and Memory info

Today I will post a short snippet on how to display cpu and memory info of our andorid device. The program uses external utilities to get the device info by calling cat on ‘/proc/meminfo’ and ‘/proc/cpuinfo’.
The activity displaying the data from the ‘cat’ output is displayed in a TextView contained withing ‘ScrollView’.

This is the output of the cat command running in emulator on my machine:

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
<LinearLayout
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 >
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textColor="#FF0000"
 android:text="CPU Info:"
 />
<TextView
 android:id="@+id/cpuinfo"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textColor="#0000FF"
 android:text="Memory Info:"
 />
<TextView
 android:id="@+id/memoryinfo"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
</LinearLayout>
</ScrollView>

Main activity:

package com.softwarepassion;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class CpuSpeed extends Activity {
private TextView cpuInfo;
private TextView memoryInfo;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cpuInfo = (TextView) findViewById(R.id.cpuinfo);
cpuInfo.setText(getCPUinfo());
memoryInfo = (TextView)findViewById(R.id.memoryinfo);
memoryInfo.setText(getMemoryInfo());
}

private String getMemoryInfo(){
ProcessBuilder cmd;
String result = new String();

try{
String[] args = {"/system/bin/cat", "/proc/meminfo"};
cmd = new ProcessBuilder(args);

Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}
private String getCPUinfo()
{
ProcessBuilder cmd;
String result="";

try{
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
cmd = new ProcessBuilder(args);

Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}
}

You don’t need to modify anything inside AndroidManifest.xml file.

One response on “Android Series: Display device CPU and Memory info

Leave a Reply