免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2051 | 回复: 0
打印 上一主题 下一主题

Android 的cpu 硬盘 内存 网络设置 系统信息 硬件信息 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-07 21:02 |只看该作者 |倒序浏览
1.手机信息查看助手可行性分析
  开始进入编写程序前,需要对需求的功能做一些可行性分析,以做到有的放矢,如果有些无法实现的功能,可以尽快调整。

  这里分析一下项目需要的功能,主要是信息查看和信息收集,如版本信息、硬件信息等,这些都可以通过读取系统文件或者运行系统命令获取,而像获取安装的软件信息和运行时信息则需要通过API提供的接口获取。实现API接口不是什么问题,主要把精力集中在如何实现运行系统命令,获取其返回的结果功能实现上。具体实现代码如下所示:
  1. public class CMDExecute {
  2.     public synchronized String run(String [] cmd, String workdirectory) throws IOException {
  3.         String result = "";
  4.        try {
  5.             ProcessBuilder builder = new ProcessBuilder(cmd);
  6.             InputStream in = null;
  7.             //设置一个路径            if (workdirectory != null) {
  8.                 builder.directory(new File(workdirectory));
  9.                 builder.redirectErrorStream(true);
  10.                 Process process = builder.start();
  11.                 in = process.getInputStream();
  12.                byte[] re = new byte[1024];
  13.                while (in.read(re) != -1)
  14.                    result = result + new String(re);
  15.             }
  16.            if (in != null) {
  17.                 in.close();
  18.                 }
  19.         } catch (Exception ex) {
  20.             ex.printStackTrace();
  21.        }  
  22.       return result;    }}
复制代码
1.2 手机信息查看助手功能实现
1.2.1 手机信息查看助手主界面
  按照预设的规划,将4类信息的查看入口放在主界面上,其布局文件为main.xml,基本上是用一个列表组件组成的,实现代码如下所示:

在这里main.xml中使用的是LinearLayout布局,其中放置了一个ListView组件。


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:/orientation="vertical" android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ListView         android:layout_width="fill_parent"        android:layout_height="fill_parent"         android:id="@+id/itemlist" /></LinearLayout>


1.2.2 查看系统信息实现
  当在运行的主界面单击第一行时,也就是“系统信息”这一行,将执行代码如下:
  1. 1 case 0:
  2. 2   intent.setClass(eoeInfosAssistant.this, System.class);
  3. 3   startActivity(intent);
  4. 4   break;
复制代码
代码运行后将显示系统(System)这个界面,这就是查看系统信息的主界面,其和主界面差不多,也就是列表显示几个需要查看的系统信息
1.2.2.1 操作系统版本
单击图9所示(无图)的界面第一行“操作系统版本”项,则会打开一个新的界面,其对应的是ShowInfo.java文件,然后需要显示该设备的操作系统版本信息,而这个信息在/proc/version中有,可以直接调用。在可行性分析中给出的CMDExencute类来调用系统的cat命令获取该文件的内容,实现代码如下:
  1. 1 public static String fetch_version_info() {
  2. 2     String result = null;
  3. 3     CMDExecute cmdexe = new CMDExecute();
  4. 4     try {
  5. 5         String[ ] args = {"/system/bin/cat", "/proc/version"};
  6. 6         result = cmdexe.run(args, "system/bin/");
  7. 7     } catch (IOException ex) {
  8. 8         ex.printStackTrace();
  9. 9     }
  10. 10     return result;
  11. 11 }
复制代码
上述代码使用的是CMDExecute类,调用系统的“"/system/bin/cat"”工具,获取“"/proc/version"”中内容。其运行效果如图9。从图中显示的查寻结果可以看到,这个设备的系统版本是Linux version 2.6.25-018430-gfea26b0。
1.2.2.2 系统信息
  在Android中,想要获取系统信息,可以调用其提供的方法System.getProperty(propertyStr),而系统信息诸如用户根目录(user.home)等都可以通过这个方法获取,实现代码如下:
  1. 1 public static StringBuffer buffer = null;
  2. 2  
  3. 3  private static String initProperty(String description,String propertyStr) {
  4. 4     if (buffer == null) {
  5. 5         buffer = new StringBuffer();
  6. 6     }
  7. 7     buffer.append(description).append(":");
  8. 8     buffer.append (System.getProperty(propertyStr)).append("\n");
  9. 9     return buffer.toString();
  10. 10 }
  11. 11
  12. 12  private static String getSystemProperty() {
  13. 13     buffer = new StringBuffer();
  14. 14     initProperty("java.vendor.url","java.vendor.url");
  15. 15     initProperty("java.class.path","java.class.path");
  16. 16     ...
  17. 17     return buffer.toString();
  18. 18 }
复制代码
上述代码主要是通过调用系统提供的System.getProperty方法获取指定的系统信息,并合并成字符串返回。

1.2.2.3 运营商信息
  运营商信息中包含IMEI、手机号码等,在Android中提供了运营商管理类(TelephonyManager),可以通过TelephonyManager来获取运营商相关的信息,实现的关键代码如下:
  1. 1 public static String fetch_tel_status(Context cx) {
  2. 2     String result = null;
  3. 3     TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
  4. 4     String str = " ";
  5. 5     str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
  6. 6     str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()+"\n";
  7. 7     // TODO: Do something ...
  8. 8      int mcc = cx.getResources().getConfiguration().mcc;
  9. 9     int mnc = cx.getResources().getConfiguration().mnc;
  10. 10     str +="IMSI MCC (Mobile Country Code): " +String.valueOf(mcc) + "\n";
  11. 11     str +="IMSI MNC (Mobile Network Code): " +String.valueOf(mnc) + "\n";
  12. 12     result = str;
  13. 13     return result;
  14. 14 }
复制代码
在上述的代码中,首先调用系统的getSystemService (Context.TELEPHONY_SERVICE)方法获取一个TelephonyManager对象tm,进而调用其方法getDeviceId()获取DeviceId信息,调用getDeviceSoftware Version()获取设备的软件版本信息等。

1.2.3 查看硬件信息
1.2.3.1 获取CPU信息
  可以在手机设备的/proc/cpuinfo中获取CPU信息,调用CMDEexecute执行系统的cat的命令,读取/proc/cpuinfo的内容,显示的就是其CPU信息,实现代码如下:

  1. 1 public static String fetch_cpu_info() {
  2. 2     String result = null;
  3. 3     CMDExecute cmdexe = new CMDExecute();
  4. 4     try {
  5. 5         String[ ] args = {"/system/bin/cat", "/proc/cpuinfo"};
  6. 6         result = cmdexe.run(args, "/system/bin/");
  7. 7         Log.i("result", "result=" + result);
  8. 8     } catch (IOException ex) {
  9. 9         ex.printStackTrace();
  10. 10     }
  11. 11     return result;
  12. 12 }
复制代码
上述代码使用CMDExecute,调用系统中的"/system/bin/cat"命令查看"/proc/cpuinfo"中的内容,即可得到CPU信息。

1.2.3.2 获取内存信息
  获取内存信息的方法和获取CPU信息的实现差不多,可以读取/proc/meminfo信息,另外还可以通过getSystemService(Context.ACTIVIT_SERV-
ICE)获取ActivityManager.MemoryInfo对象,进而获取可用内存信息,主要代码如下:

View Code
  1. 1 /**
  2. 2  *系统内存情况查看
  3. 3  */
  4. 4  public static String getMemoryInfo(Context context) {
  5. 5     StringBuffer memoryInfo = new StringBuffer();
  6. 6      
  7. 7     final ActivityManager activityManager =  
  8. 8         (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  9. 9     ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
  10. 10     activityManager.getMemoryInfo(outInfo);
  11. 11     
  12. 12     memoryInfo.append("\nTotal Available Memory :").append(outInfo.availMem >> 10).append("k");
  13. 13     memoryInfo.append("\nTotal Available Memory :").append(outInfo.availMem >> 20).append("k");
  14. 14     memoryInfo.append("\nIn low memory situation:").append(outInfo.lowMemory);
  15. 15     
  16. 16     String result = null;
  17. 17     CMDExecute cmdexe = new CMDExecute();
  18. 18     try {
  19. 19         String[ ] args = {"/system/bin/cat", "/proc/meminfo"};
  20. 20         result = cmdexe.run(args, "/system/bin/");
  21. 21     } catch (IOException ex) {
  22. 22         Log.i("fetch_process_info","ex=" + ex.toString());
  23. 23     }
  24. 24     return (memoryInfo.toString() + "\n\n" + result);
  25. 25 }
复制代码
上述代码中首先通过ActivityManager对象获取其可用的内存,然后通过查看“/proc/meminfo”内容获取更详细的信息。
1.2.3.3 获取磁盘信息
  手机设备的磁盘信息可以通过df命令获取,所以,这里获取磁盘信息的方法和前面类似,惟一不同的是,这个是直接执行命令,获取其命令的返回就可以了,关键代码如下:
  1. //磁盘信息public static String fetch_disk_info() {
  2.     String result = null;
  3.     CMDExecute cmdexe = new CMDExecute();
  4.     try {
  5.         String[ ] args = {"/system/bin/df"};
  6.         result = cmdexe.run(args, "/system/bin/");
  7.         Log.i("result", "result=" + result);
  8.     } catch (IOException ex) {
  9.         ex.printStackTrace();
  10.     }
  11.     return result;}
复制代码
1.2.3.4 获取网络信息
要获取手机设备的网络信息,只要读取/system/bin/netcfg中的信息就可以了,关键代码如下:

  1. public static String fetch_netcfg_info() {
  2.     String result = null;    CMDExecute cmdexe = new CMDExecute();
  3.         try {
  4.         String[ ] args = {"/system/bin/netcfg"};
  5.         result = cmdexe.run(args, "/system/bin/");
  6.     } catch (IOException ex) {
  7.         Log.i("fetch_process_info","ex=" + ex.toString());
  8.     }
  9.    return result;}
复制代码
1.2.3.5获取显示频信息
  除了显示手机的CPU、内存、磁盘信息外,还有个非常重要的硬件,显示频。在Android中,它提供了DisplayMetrics类,可以通过getApplication Context()、getResources()、getDisplayMetrics()初始化,进而读取其屏幕宽(widthPixels)、高(heightPixels)等信息,实现的关键代码如下:


View Code
  1. 1 public static String getDisplayMetrics(Context cx) {
  2. 2     String str = "";
  3. 3     DisplayMetrics dm = new DisplayMetrics();
  4. 4     dm = cx.getApplicationContext().getResources().getDisplayMetrics();
  5. 5     int screenWidth = dm.widthPixels;
  6. 6     int screenHeight = dm.heightPixels;
  7. 7     float density = dm.density;
  8. 8     float xdpi = dm.xdpi;
  9. 9     float ydpi = dm.ydpi;
  10. 10     str += "The absolute width: " + String.valueOf(screenWidth) + "pixels\n";
  11. 11     str += "The absolute heightin: " + String.valueOf(screenHeight) + "pixels\n";
  12. 12     str += "The logical density of the display. : " + String.valueOf(density) + "\n";
  13. 13     str += "X dimension : " + String.valueOf(xdpi) +"pixels per inch\n";14     str += "Y dimension : " + String.valueOf(ydpi) +"pixels per inch\n";15     return str;16 }
复制代码
1.2.4 查看软件信息
在Android上,可以在手机上随便安装自己喜欢的应用软件,查看软件信息的功能就是收集并显示已经安装的应用软件信息。Android提供了getPackageManager()、getInstalledApplications(0)方法,可以直接返回全部已经安装的应用列表。这个功能就是只需要获取列表,再进行显示在列表中就可以了。但是,如果安装的软件比较多,那么获取信息所花费的时间会比较多,为了更好地完善用户使用的体验,在获取列表时,需要在界面提示用户耐心等待,这就需要用到Android提供的另外一个功能Runnable。
引入Runnable比较简单,只需要在定义类的时候实现Runnable接口就可以了,所以,这里的软件信息查看界面对应的Software.java类声明代码如下:
public class Software extends Activity implements Runnable {
. . .
}
然后需要在这个Activity启动的时候,引入进度条ProgressDialog来显示一个提示界面,onCreate代码如下所示:
  1. 1 public void onCreate(Bundle icicle) {
  2. 2     Super.onCreate(icicle);
  3. 3     setContentView(R.layout.softwares);
  4. 4     setTitle("软件信息");
  5. 5     itemlist = (ListView) findViewById(R.id.itemlist);
  6. 6     pd = ProgressDialog.show(this, "请稍候. .", "正在收集你已经安装的软件信息. . .", true, false);
  7. 7     Thread thread = new Thread(this);8     thread.start();9 }
复制代码
该方法创建了一个ProgressDialog,并设定其提示信息。然后实现其线程的run()方法,该方法实现其真正执行的逻辑,实现代码如下:
  1. @Override
  2. Public void run() {
  3.     fetch_installed_apps();
  4.     handler.sendEmptyMessage(0);
  5. }
复制代码
上述代码调用自定义的fetch_installed_app()方法获取已经安装的应用信息,这个方法是比较消耗时间的,实现代码如下:

View Code
  1. 1     public ArrayList fetch_installed_apps () {
  2. 2         List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0);
  3. 3         ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(packages.size());
  4. 4         
  5. 5         Iterator<ApplicationInfo> l = packages.iterator();
  6. 6         while (l.hasNext()) {
  7. 7             HashMap<String, Object> map = new HashMap<String, Object>();
  8. 8             ApplicationInfo app = (ApplicationInfo) l.next();
  9. 9             String packageName = app.packageName;
  10. 10             String label = " ";
  11. 11             try {
  12. 12                label = getPackageManager().getApplicationLabel(app).toString();
  13. 13             } catch (Exception e) {
  14. 14                 Log.i("Exception", e.toString()
  15. 15             );
  16. 16         }
  17. 17         map = new HashMap<String, Object>();
  18. 18         map.put("name", label);
  19. 19         map.put("desc", packageName);
  20. 20         list.add(map);
  21. 21         }
  22. 22         return list;
  23. 23     }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP