Mar 17, 2015

Get number of available cores and cpu info

The method Runtime.availableProcessors() returns the number of processor cores available to the VM.

And the /proc/cpuinfo hold infoprmation about the CPU, you can read it as text file.



Example:
package com.example.androidcpuinfo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 TextView cores, cpuinfo;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cores = (TextView)findViewById(R.id.cores);
        cpuinfo = (TextView)findViewById(R.id.cpuinfo);
        
        Runtime runtime = Runtime.getRuntime();
        int availableProcessors = runtime.availableProcessors();
        cores.setText("You have " + availableProcessors + " availableProcessors");
        
        //Read text file "/proc/cpuinfo"
        String file_cpuinfo = "/proc/cpuinfo";
        String info = "";
        try {
   FileReader fileReader = new FileReader(file_cpuinfo);
   BufferedReader bufferReader = new BufferedReader(fileReader);

   String line;
         try {
    while((line = bufferReader.readLine()) != null)
    {
     info += line + "\n";
    }
    cpuinfo.setText(info);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
         
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidcpuinfo.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="24dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/cores"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textStyle="italic" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/cpuinfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads