Monday, September 7, 2009

Read Android CPU info

Android CPU info. can be retrieved by reading of "/proc/cpuinfo". So, it's just a very little bit of modification on Read Android OS version.



Create a new Android Application, with the Activity named AndroidCPUinfoActivity. Modify the main.xml and AndroidCPUinfoActivity.java:

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="android-er.blogspot.com"
android:autoLink="web"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android CPU Info.:"
/>
<TextView
android:id="@+id/CPUinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Download main.xml.

AndroidCPUinfoActivity.java
package com.exercise.AndroidCPUinfo;

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

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

public class AndroidCPUinfoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView CPUinfo = (TextView) findViewById(R.id.CPUinfo);
CPUinfo.setText(ReadCPUinfo());

}

private String ReadCPUinfo()
{
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;
}
}

Download AndroidCPUinfoActivity.java.

4 comments:

Shubham said...

do u have any idea what BogoMIPS indicate here ?...i want to know on what processor my mob is working...any help?

Unknown said...

I think there's no need for cat here. Reading /proc/cpuinfo directly with java tools is a much better idea.

Unknown said...

Can you add statistics of frequencies?

Unknown said...

Not sure if possible, but would love to see process level stats.