I want to have the same details in my android app. Anybody having any solution?
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:
First, i've tried to parse the "top" command output to get overal usage, and my onw app. but I found that the results are totally different if I run the top from adb shell, or from the app. The app reported about a half CPU usage that was at adb (and android profiler tab too). So using top is not a solution.
As I see it, you need to parse the "/proc/stat" file. Thanks to this question ,and code provided by Souch the job is already done for us. Just use his CpuInfo class to get the average load each time you need it.
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:
Use this function to get CPU details.
public static String getCPUDetails(){ /* *Created By Atiar Talukdar * 01/01/2018 * contact@atiar.info */ ProcessBuilder processBuilder; String cpuDetails = ""; String[] DATA = {"/system/bin/cat", "/proc/cpuinfo"}; InputStream is; Process process ; byte[] bArray ; bArray = new byte[1024]; try{ processBuilder = new ProcessBuilder(DATA); process = processBuilder.start(); is = process.getInputStream(); while(is.read(bArray) != -1){ cpuDetails = cpuDetails + new String(bArray); //Stroing all the details in cpuDetails } is.close(); } catch(IOException ex){ ex.printStackTrace(); } return cpuDetails; }
Expected Output like - (I Used Emulator to generate output)
vendor_id : GenuineIntel
cpu family : 6
model : 61
model name : Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
stepping : 4
cpu MHz : 2699.883
cache size : 3072 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm
constant_tsc pebs bts rep_good nopl eagerfpu pni pclmulqdq dtes64 ds_cpl ssse3
sdbg fma cx16 xtpr pdcm movbe popcnt aes xsave avx f16c rdrand hypervisor
lahf_lm retpoline fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms xsaveopt
bugs : cpu_meltdown spectre_v1 spectre_v2
bogomips : 5399.76
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 61
model name : Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
stepping : 4
cpu MHz : 2699.883
cache size : 3072 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm
constant_tsc pebs bts rep_good nopl eagerfpu pni pclmulqdq dtes64 ds_cpl ssse3
sdbg fma cx16 xtpr pdcm movbe popcnt aes xsave avx f16c rdrand hypervisor
lahf_lm retpoline fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms xsaveopt
bugs : cpu_meltdown spectre_v1 spectre_v2
bogomips : 5399.76
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
s : cpu_meltdown spectre_v1 spectre_v2
bogomips : 5399.76
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 61
model name : Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
stepping : 4
cpu MHz : 2699.883
cache size : 3