• 查看Android设备的CPU架构信息


    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输入流读入就可以了

  • 相关阅读:
    SSH框架总结(框架分析+环境搭建+实例源码下载)(转)
    用PowerMockito来mock私有方法(转)
    Mockito简介(转)
    统治世界的十大算法
    ThreadLocal用法和实现原理(转)
    在Eclipse中使用JUnit4进行单元測试(0基础篇)
    libgdx, mouse 关节
    sprintf,你知道多少?
    北京簋街 美食全然攻略 + 簋街好吃的夜宵去处-----店铺介绍大全
    codeforces-148D-Bag of mice-概率DP
  • 原文地址:https://www.cnblogs.com/myPersonalTailor/p/4872303.html
Copyright © 2020-2023  润新知