How to find CPU load of any Android device programmatically

后端 未结 4 1273
南方客
南方客 2020-12-05 20:51

I want to have the same details in my android app. Anybody having any solution?

4条回答
  •  天命终不由人
    2020-12-05 21:23

    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:

    • How to get CPU usage statistics on Android?
    • Get cpu info programmatically on android application
    • How I get CPU usage and temperature information into an android app?
    • https://www.android-examples.com/get-android-device-cpu-information-programmatically/

提交回复
热议问题