答案 0 :(得分:5)
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);
}
}
更多信息:
答案 1 :(得分:0)
首先,我尝试解析“ top”命令输出以获取总体使用情况以及我的onw应用程序。但是我发现,如果我从adb shell或应用程序运行顶部,结果将完全不同。 该应用程序报告了adb的一半CPU使用率(也是android profiler选项卡)。因此,使用top并不是解决方案。
如我所见,您需要解析“ / proc / stat”文件。 感谢Souch提供的this question和code,这项工作已经为我们完成了。只需使用他的CpuInfo类即可在每次需要时获取平均负载。
答案 2 :(得分:0)
由于Android基于Linux内核的修改版,因此我们可以根据documentation使用相同的Linux命令来检索CPU信息。
此虚拟文件标识系统使用的处理器类型- / proc / cpuinfo
命令可以通过ADB shell执行
ADB命令:
adb shell cat /proc/cpuinfo
使用Java API中的ProcessBuilder
,您可以从Android应用程序中执行以下shell命令。
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();
}
示例输出:
答案 3 :(得分:-1)
使用此功能获取CPU详细信息。
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; }
预期的输出,例如-(我使用仿真器生成输出)
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