查看Android设备的CPU架构信息

时间:2024-03-09 13:52:32

android系统其实是linux,那么可以在程序中去调用cat /proc/meminfo和cat 
/proc/cpuino去查看内存和CPU等情况的,下面是程序:

  1. public class CpuSpeed extends Activity {
  2. /** Called when the activity is first created. */
  3.  
  4. private TextView cpuInfo;
  5. private TextView memoryInfo;
  6.  
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. cpuInfo=(TextView)findViewById(R.id.cpuinfo);
  11. cpuInfo.setText(getCPUinfo());
  12. memoryInfo = (TextView)findViewById(R.id.memoryinfo);
  13. memoryInfo.setText(getMemoryInfo());
  14.  
  15. }
  16. private String getMemoryInfo(){
  17. ProcessBuilder cmd;
  18. String result = new String();
  19.  
  20. try{
  21. String[] args = {"/system/bin/cat", "/proc/meminfo"};
  22. cmd = new ProcessBuilder(args);
  23.  
  24. Process process = cmd.start();
  25. InputStream in = process.getInputStream();
  26. byte[] re=new byte[1024];
  27. while (in.read(re)!=-1)
  28. {
  29. System.out.println(new String(re));
  30. result = result + new String(re);
  31.  
  32. }
  33. in.close();
  34. }
  35. catch(IOException ex){
  36. ex.printStackTrace();
  37. }
  38. return result;
  39.  
  40. }
  41. private String getCPUinfo()
  42. {
  43. ProcessBuilder cmd;
  44. String result="";
  45.  
  46. try{
  47. String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
  48. cmd = new ProcessBuilder(args);
  49.  
  50. Process process = cmd.start();
  51. InputStream in = process.getInputStream();
  52. byte[] re = new byte[1024];
  53. while(in.read(re) != -1){
  54. System.out.println(new String(re));
  55. result = result + new String(re);
  56. }
  57. in.close();
  58. } catch(IOException ex){
  59. ex.printStackTrace();
  60. }
  61. return result;
  62. }
 

}

其实核心无非就是ProcessBuilder的运用,去启动命令行去读操作系统, 
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};

  1. cmd = new ProcessBuilder(args);
  2.  
  3. Process process = cmd.start();
  4. InputStream in = process.getInputStream();
 

然后再IO输入流读入就可以了