How to get processor speed and RAM in android device

前端 未结 2 1524
情书的邮戳
情书的邮戳 2021-01-13 01:35

Could anyone help me how to get processor name, speed and RAM of Android device via code.

相关标签:
2条回答
  • 2021-01-13 02:21

    its only possible on a rooted device or your app is running as system application.

    For wanted informations you have to look in the running kernel, as i know this informations cant be obtained by the android system itself.

    To obtain informations about the CPU, you can read and parse this file: /proc/cpuinfo

    To obtain memory informations, you can read and parse this file: /proc/memory

    0 讨论(0)
  • 2021-01-13 02:34

    You can get processor, RAM and other hardware related information as we normally get in Linux. From terminal we can issue these command in a normal Linux system. You don't need to have a rooted device for this.

    $ cat /proc/cpuinfo 
    

    Similarly you can issue these commands in android code and get the result.

    public void getCpuInfo() {
        try {
            Process proc = Runtime.getRuntime().exec("cat /proc/cpuinfo");
            InputStream is = proc.getInputStream();
            TextView tv = (TextView)findViewById(R.id.tvcmd);
            tv.setText(getStringFromInputStream(is));
        } 
        catch (IOException e) {
            Log.e(TAG, "------ getCpuInfo " + e.getMessage());
        }
    }
    
    public void getMemoryInfo() {
        try {
            Process proc = Runtime.getRuntime().exec("cat /proc/meminfo");
            InputStream is = proc.getInputStream();
            TextView tv = (TextView)findViewById(R.id.tvcmd);
            tv.setText(getStringFromInputStream(is));
        } 
        catch (IOException e) {
            Log.e(TAG, "------ getMemoryInfo " + e.getMessage());
        }
    }
    
    private static String getStringFromInputStream(InputStream is) {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
    
        try {
            while((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } 
        catch (IOException e) {
            Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
        }
        finally {
            if(br != null) {
                try {
                    br.close();
                } 
                catch (IOException e) {
                    Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
                }
            }
        }       
    
        return sb.toString();
    }
    
    0 讨论(0)
提交回复
热议问题