Saturday, September 5, 2009

Read Android OS version

In this exercise, read Android OS version, using ProcessBuilder to Creates operating system processes.



Create a operating system processes, "/system/bin/cat", to retrieve "/proc/version".

If your system is running in Linux, you can try to type the command below in Terminal.
$ cat /proc/version



Actually, this exercise perform the same operation.

Create a new Android Application, with the Activity named AndroidOSinfoActivity. Modify the main.xml and AndroidOSinfoActivity.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 OS:"
/>
<TextView
android:id="@+id/OSinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Download main.xml.

AndroidOSinfoActivity.java
package com.exercise.AndroidOSinfo;

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

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

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

TextView OSinfo = (TextView) findViewById(R.id.OSinfo);
OSinfo.setText(ReadOSinfo());

}

private String ReadOSinfo()
{
ProcessBuilder cmd;
String result="";

try{
String[] args = {"/system/bin/cat", "/proc/version"};
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 AndroidOSinfoActivity.java.

It's another exercise to Read Android system info., using System.getProperty.

No comments:

Post a Comment