標籤:
android系統其實是linux,那麼可以在程式中去調用cat /proc/meminfo和cat
/proc/cpuino去查看記憶體和CPU等情況的,下面是程式:
- public class CpuSpeed extends Activity {
- /** Called when the activity is first created. */
-
- private TextView cpuInfo;
- private TextView memoryInfo;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- cpuInfo=(TextView)findViewById(R.id.cpuinfo);
- cpuInfo.setText(getCPUinfo());
- memoryInfo = (TextView)findViewById(R.id.memoryinfo);
- memoryInfo.setText(getMemoryInfo());
-
- }
- private String getMemoryInfo(){
- ProcessBuilder cmd;
- String result = new String();
-
- try{
- String[] args = {"/system/bin/cat", "/proc/meminfo"};
- cmd = new ProcessBuilder(args);
-
- Process process = cmd.start();
- InputStream in = process.getInputStream();
- byte[] re=new byte[1024];
- while (in.read(re)!=-1)
- {
- System.out.println(new String(re));
- result = result + new String(re);
-
- }
- in.close();
- }
- catch(IOException ex){
- ex.printStackTrace();
- }
- return result;
-
- }
- private String getCPUinfo()
- {
- ProcessBuilder cmd;
- String result="";
-
- try{
- String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
- cmd = new ProcessBuilder(args);
-
- Process process = cmd.start();
- InputStream in = process.getInputStream();
- byte[] re = new byte[1024];
- while(in.read(re) != -1){
- System.out.println(new String(re));
- result = result + new String(re);
- }
- in.close();
- } catch(IOException ex){
- ex.printStackTrace();
- }
- return result;
- }
}
其實核心無非就是ProcessBuilder的運用,去啟動命令列去讀作業系統,
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
- cmd = new ProcessBuilder(args);
-
- Process process = cmd.start();
- InputStream in = process.getInputStream();
然後再IO輸入資料流讀入就可以了
查看Android裝置的CPU架構資訊