您的位置:首页 > 理论基础 > 计算机网络

【转】Android 的cpu 硬盘 内存 网络设置 系统信息 硬件信息

2011-04-07 14:54 579 查看
1.手机信息查看助手可行性分析
  开始进入编写程序前,需要对需求的功能做一些可行性分析,以做到有的放矢,如果有些无法实现的功能,可以尽快调整。

  这里分析一下项目需要的功能,主要是信息查看和信息收集,如版本信息、硬件信息等,这些都可以通过读取系统文件或者运行系统命令获取,而像获取安装的软件信息和运行时信息则需要通过API提供的接口获取。实现API接口不是什么问题,主要把精力集中在如何实现运行系统命令,获取其返回的结果功能实现上。具体实现代码如下所示:

public class CMDExecute {

public synchronized String run(String [] cmd, String workdirectory) throws IOException {
String result = "";
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
//设置一个路径
if (workdirectory != null) {
builder.directory(new File(workdirectory));
builder.redirectErrorStream(true);
Process process = builder.start();
in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1)
result = result + new String(re);
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
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 查看系统信息实现
  当在运行的主界面单击第一行时,也就是“系统信息”这一行,将执行代码如下:

case 0:
  intent.setClass(eoeInfosAssistant.this, System.class);
  startActivity(intent);
  break;


代码运行后将显示系统(System)这个界面,这就是查看系统信息的主界面,其和主界面差不多,也就是列表显示几个需要查看的系统信息

1.2.2.1 操作系统版本
单击图9所示(无图)的界面第一行“操作系统版本”项,则会打开一个新的界面,其对应的是ShowInfo.java文件,然后需要显示该设备的操作系统版本信息,而这个信息在/proc/version中有,可以直接调用。在可行性分析中给出的CMDExencute类来调用系统的cat命令获取该文件的内容,实现代码如下:

public static String fetch_version_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[ ] args = {"/system/bin/cat", "/proc/version"};
result = cmdexe.run(args, "system/bin/");
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}


  上述代码使用的是CMDExecute类,调用系统的“"/system/bin/cat"”工具,获取“"/proc/version"”中内容。其运行效果如图9。从图中显示的查寻结果可以看到,这个设备的系统版本是Linux version 2.6.25-018430-gfea26b0。

1.2.2.2 系统信息
  在Android中,想要获取系统信息,可以调用其提供的方法System.getProperty(propertyStr),而系统信息诸如用户根目录(user.home)等都可以通过这个方法获取,实现代码如下:

public static StringBuffer buffer = null;

private static String initProperty(String description,String propertyStr) {
if (buffer == null) {
buffer = new StringBuffer();
}
buffer.append(description).append(":");
buffer.append (System.getProperty(propertyStr)).append("\n");
return buffer.toString();
}

private static String getSystemProperty() {
buffer = new StringBuffer();
initProperty("java.vendor.url","java.vendor.url");
initProperty("java.class.path","java.class.path");
...
return buffer.toString();
}


上述代码主要是通过调用系统提供的System.getProperty方法获取指定的系统信息,并合并成字符串返回。

1.2.2.3 运营商信息
  运营商信息中包含IMEI、手机号码等,在Android中提供了运营商管理类(TelephonyManager),可以通过TelephonyManager来获取运营商相关的信息,实现的关键代码如下:

public static String fetch_tel_status(Context cx) {
String result = null;
TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
String str = " ";
str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()+"\n";
// TODO: Do something ...
int mcc = cx.getResources().getConfiguration().mcc;
int mnc = cx.getResources().getConfiguration().mnc;
str +="IMSI MCC (Mobile Country Code): " +String.valueOf(mcc) + "\n";
str +="IMSI MNC (Mobile Network Code): " +String.valueOf(mnc) + "\n";
result = str;
return result;
}


  在上述的代码中,首先调用系统的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信息,实现代码如下:

public static String fetch_cpu_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[ ] args = {"/system/bin/cat", "/proc/cpuinfo"};
result = cmdexe.run(args, "/system/bin/");
Log.i("result", "result=" + result);
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}


上述代码使用CMDExecute,调用系统中的"/system/bin/cat"命令查看"/proc/cpuinfo"中的内容,即可得到CPU信息。

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

View Code

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "item clicked! [" + position + "]");
if (position == 0) {
path = "/";
refreshListItems(path);
}else if(position ==1) {
goToParent();
} else {
path = (String) list.get(position).get("path");
File file = new File(path);
if (file.isDirectory())
refreshListItems(path);
else
Toast.makeText(FSExplorer.this,getString(R.string.is_file), Toast.LENGTH_SHORT).show();
}
}


2. Android编程获取手机型号,網絡類型,本机电话号码,sdk版本及firmware版本号(即系统版本号)

Android开发平台中,可通过TelephonyManager获取本机号码。

TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
txtPhoneNumber.setText(phoneMgr.getLine1Number()); //txtPhoneNumber是一个EditText 用于显示手机号

注:

根据Android的安全机制,在使用TelephonyManager时,必须在AndroidManifest.xml中添加<uses-permission android:name="READ_PHONE_STATE" /> 否则无法获得系统的许可。

private void loadPhoneStatus()
{
TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);

int mNetworkType = mManager.getNetworkType();//獲取網絡類型
txtPhoneModel.setText(Build.MODEL); //手机型号
txtPhoneNumber.setText(phoneMgr.getLine1Number());//本机电话号码
txtSdkVersion.setText(Build.VERSION.SDK);//SDK版本号
txtOsVersion.setText(Build.VERSION.RELEASE);//Firmware/OS 版本号
}

Build能向我们提供包括 硬件厂商,硬件编号,序列号等很多信息 调用方法也都同上.

3. 得到系統的一些相關的屬性,通過調用 System.getProperties(); 來獲取

02-09 02:24:55.863: INFO/Tag(661): System Property: {java.vm.version=1.2.0
02-09 02:24:55.863: INFO/Tag(661): System Property: java.vendor.url=http://www.android.com/
02-09 02:24:55.863: INFO/Tag(661): System Property: java.vm.vendor.url=http://www.android.com/
02-09 02:24:55.863: INFO/Tag(661): System Property: user.dir=/
02-09 02:24:55.872: INFO/Tag(661): System Property: java.vm.name=Dalvik
02-09 02:24:55.872: INFO/Tag(661): System Property: java.home=/system
02-09 02:24:55.872: INFO/Tag(661): System Property: user.region=US
02-09 02:24:55.872: INFO/Tag(661): System Property: javax.net.ssl.trustStore=/system/etc/security/cacerts.bks
02-09 02:24:55.872: INFO/Tag(661): System Property: java.runtime.name=Android Runtime
02-09 02:24:55.882: INFO/Tag(661): System Property: user.home=
02-09 02:24:55.882: INFO/Tag(661): System Property: java.io.tmpdir=/sdcard
02-09 02:24:55.882: INFO/Tag(661): System Property: http.agent=Dalvik/1.2.0 (Linux; U; Android 2.2; google_sdk Build/FRF91)
02-09 02:24:55.882: INFO/Tag(661): System Property: java.net.preferIPv6Addresses=true
02-09 02:24:55.882: INFO/Tag(661): System Property: java.version=0
02-09 02:24:55.882: INFO/Tag(661): System Property: java.boot.class.path=/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar
02-09 02:24:55.882: INFO/Tag(661): System Property: java.library.path=/system/lib
02-09 02:24:55.882: INFO/Tag(661): System Property: file.separator=/
02-09 02:24:55.882: INFO/Tag(661): System Property: java.specification.vendor=The Android Project
02-09 02:24:55.894: INFO/Tag(661): System Property: file.encoding=UTF-8
02-09 02:24:55.894: INFO/Tag(661): System Property: line.separator=
02-09 02:24:55.894: INFO/Tag(661): System Property: java.vm.specification.version=0.9
02-09 02:24:55.894: INFO/Tag(661): System Property: java.vm.specification.vendor=The Android Project
02-09 02:24:55.894: INFO/Tag(661): System Property: os.name=Linux
02-09 02:24:55.894: INFO/Tag(661): System Property: java.vm.vendor=The Android Project
02-09 02:24:55.894: INFO/Tag(661): System Property: path.separator=:
02-09 02:24:55.904: INFO/Tag(661): System Property: android.vm.dexfile=true
02-09 02:24:55.904: INFO/Tag(661): System Property: java.ext.dirs=
02-09 02:24:55.904: INFO/Tag(661): System Property: java.class.path=.
02-09 02:24:55.904: INFO/Tag(661): System Property: os.version=2.6.29-00261-g0097074-dirty
02-09 02:24:55.904: INFO/Tag(661): System Property: java.specification.name=Dalvik Core Library
02-09 02:24:55.904: INFO/Tag(661): System Property: java.compiler=
02-09 02:24:55.913: INFO/Tag(661): System Property: user.language=en
02-09 02:24:55.913: INFO/Tag(661): System Property: user.name=
02-09 02:24:55.913: INFO/Tag(661): System Property: os.arch=armv5tejl
02-09 02:24:55.913: INFO/Tag(661): System Property: java.runtime.version=0.9
02-09 02:24:55.913: INFO/Tag(661): System Property: java.class.version=46.0
02-09 02:24:55.913: INFO/Tag(661): System Property: java.vendor=The Android Project
02-09 02:24:55.923: INFO/Tag(661): System Property: java.vm.specification.name=Dalvik Virtual Machine Specification
02-09 02:24:55.923: INFO/Tag(661): System Property: java.specification.version=0.9}
4. 得到鍵盤信息,使用的語言,手機的網絡代碼(mnc),手機的國家代碼(mcc),手機的模式,手機的方向,觸摸屏的判斷等,通過以下語句獲取: Configuration config = getResources().getConfiguration();

這些屬性都在config的屬性變量中進行判斷。

=====================开心的分割线=====================

特别说明:本文转载自 http://blog.163.com/ma95221@126/blog/static/24822102201121512633179/
只对代码部分的格式进行相关调整,并修正已知的代码错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐