Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find CPU load of any Android device programmatically

I want to have the same details in my android app. Anybody having any solution? enter image description here

like image 656
Arpit Agarwal Avatar asked Oct 12 '17 16:10

Arpit Agarwal


2 Answers

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    TextView textView ;
    ProcessBuilder processBuilder;
    String Holder = "";
    String[] DATA = {"/system/bin/cat", "/proc/cpuinfo"};
    InputStream inputStream;
    Process process ;
    byte[] byteArry ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);

        byteArry = new byte[1024];

        try{
            processBuilder = new ProcessBuilder(DATA);

            process = processBuilder.start();

            inputStream = process.getInputStream();

            while(inputStream.read(byteArry) != -1){

                Holder = Holder + new String(byteArry);
            }

            inputStream.close();

        } catch(IOException ex){

            ex.printStackTrace();
        }

        textView.setText(Holder);
    }
}

More info:

  • How to get CPU usage statistics on Android?
  • Get cpu info programmatically on android application
  • How I get CPU usage and temperature information into an android app?
  • https://www.android-examples.com/get-android-device-cpu-information-programmatically/
like image 55
Darush Avatar answered Oct 06 '22 09:10

Darush


Since Android is based on a modified version of the Linux kernel, the same Linux command we can use to retrieve the CPU information according to the documentation.

This virtual file identifies the type of processor used by your system - /proc/cpuinfo

The command can be executed through ADB shell

ADB Command:

adb shell cat /proc/cpuinfo

Using ProcessBuilder in Java API, you can execute the shell command from the Android application as below.

Java API:

try {

    String[] DATA = {"/system/bin/cat", "/proc/cpuinfo"};
    ProcessBuilder processBuilder = new ProcessBuilder(DATA);
    Process process = processBuilder.start();
    InputStream inputStream = process.getInputStream();
    byte[] byteArry = new byte[1024];
    String output = "";
    while (inputStream.read(byteArry) != -1) {
        output = output + new String(byteArry);
    }
    inputStream.close();

    Log.d("CPU_INFO", output);

} catch (Exception ex) {
    ex.printStackTrace();
}

Sample output:

enter image description here

like image 34
Googlian Avatar answered Oct 06 '22 08:10

Googlian