Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2559479
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Android平台

2013-04-26 11:40:57

布局文件activity_main.xml


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android=""
  3.     android:id="@+id/ScrollView01"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="wrap_content" >

  6.     <LinearLayout
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:orientation="vertical" >

  10.         <TextView
  11.             android:layout_width="fill_parent"
  12.             android:layout_height="wrap_content"
  13.             android:text="CPU Info:"
  14.             android:textColor="#FF0000" />

  15.         <TextView
  16.             android:id="@+id/cpuinfo"
  17.             android:layout_width="fill_parent"
  18.             android:layout_height="wrap_content" />

  19.         <TextView
  20.             android:layout_width="fill_parent"
  21.             android:layout_height="wrap_content"
  22.             android:text="Memory Info:"
  23.             android:textColor="#0000FF" />

  24.         <TextView
  25.             android:id="@+id/memoryinfo"
  26.             android:layout_width="fill_parent"
  27.             android:layout_height="wrap_content" />
  28.     </LinearLayout>

  29. </ScrollView>



MainActivity.java

点击(此处)折叠或打开

  1. package com.example.android;

  2. import java.io.IOException;
  3. import java.io.InputStream;

  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.widget.TextView;

  8. public class MainActivity extends Activity
  9. {
  10.     private TextView cpuInfo;
  11.     private TextView memoryInfo;

  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState)
  14.     {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         
  18.         cpuInfo = (TextView) findViewById(R.id.cpuinfo);
  19.         cpuInfo.setText(getCPUinfo());
  20.         memoryInfo = (TextView)findViewById(R.id.memoryinfo);
  21.         memoryInfo.setText(getMemoryInfo());
  22.     }

  23.     @Override
  24.     public boolean onCreateOptionsMenu(Menu menu)
  25.     {
  26.         // Inflate the menu; this adds items to the action bar if it is present.
  27.         getMenuInflater().inflate(R.menu.main, menu);
  28.         return true;
  29.     }

  30.     private String getMemoryInfo()
  31.     {
  32.         ProcessBuilder cmd;
  33.         String result = new String();

  34.         try
  35.         {
  36.             String[] args = { "/system/bin/cat", "/proc/meminfo" };
  37.             cmd = new ProcessBuilder(args);

  38.             Process process = cmd.start();
  39.             InputStream in = process.getInputStream();
  40.             byte[] re = new byte[1024];
  41.             while (in.read(re) != -1)
  42.             {
  43.                 System.out.println(new String(re));
  44.                 result = result + new String(re);
  45.             }
  46.             in.close();
  47.         }
  48.         catch (IOException ex)
  49.         {
  50.             ex.printStackTrace();
  51.         }
  52.         return result;
  53.     }

  54.     private String getCPUinfo()
  55.     {
  56.         ProcessBuilder cmd;
  57.         String result = "";

  58.         try
  59.         {
  60.             String[] args = { "/system/bin/cat", "/proc/cpuinfo" };
  61.             cmd = new ProcessBuilder(args);

  62.             Process process = cmd.start();
  63.             InputStream in = process.getInputStream();
  64.             byte[] re = new byte[1024];
  65.             while (in.read(re) != -1)
  66.             {
  67.                 System.out.println(new String(re));
  68.                 result = result + new String(re);
  69.             }
  70.             in.close();
  71.         }
  72.         catch (IOException ex)
  73.         {
  74.             ex.printStackTrace();
  75.         }
  76.         return result;
  77.     }

  78. }


效果图:

阅读(1202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~