赞
踩
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 查看系统信息实现
当在运行的主界面单击第一行时,也就是“系统信息”这一行,将执行代码如下:
1 case 0 : 2 intent.setClass(eoeInfosAssistant. this , System. class ); 3 startActivity(intent); 4 break ;
1.2.2.1 操作系统版本
单击图9所示(无图)的界面第一行“操作系统版本”项,则会打开一个新的界面,其对应的是ShowInfo.java文件,然后需要显示该设备的操作系统版本信息,而这个信息在/proc/version中有,可以直接调用。在可行性分析中给出的CMDExencute类来调用系统的cat命令获取该文件的内容,实现代码如下:
1 public static String fetch_version_info() { 2 String result = null ; 3 CMDExecute cmdexe = new CMDExecute(); 4 try { 5 String[ ] args = { " /system/bin/cat " , " /proc/version " }; 6 result = cmdexe.run(args, " system/bin/ " ); 7 } catch (IOException ex) { 8 ex.printStackTrace(); 9 } 10 return result; 11 }
1.2.2.2 系统信息
在Android中,想要获取系统信息,可以调用其提供的方法System.getProperty(propertyStr),而系统信息诸如用户根目录(user.home)等都可以通过这个方法获取,实现代码如下:
1 public static StringBuffer buffer = null ; 2 3 private static String initProperty(String description,String propertyStr) { 4 if (buffer == null ) { 5 buffer = new StringBuffer(); 6 } 7 buffer.append(description).append( " : " ); 8 buffer.append (System.getProperty(propertyStr)).append( " \n " ); 9 return buffer.toString(); 10 } 11 12 private static String getSystemProperty() { 13 buffer = new StringBuffer(); 14 initProperty( " java.vendor.url " , " java.vendor.url " ); 15 initProperty( " java.class.path " , " java.class.path " ); 16 ... 17 return buffer.toString(); 18 }
1.2.2.3 运营商信息
运营商信息中包含IMEI、手机号码等,在Android中提供了运营商管理类(TelephonyManager),可以通过TelephonyManager来获取运营商相关的信息,实现的关键代码如下:
1 public static String fetch_tel_status(Context cx) { 2 String result = null ; 3 TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE); 4 String str = " " ; 5 str += " DeviceId(IMEI) = " + tm.getDeviceId() + " \n " ; 6 str += " DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + " \n " ; 7 // TODO: Do something ... 8 int mcc = cx.getResources().getConfiguration().mcc; 9 int mnc = cx.getResources().getConfiguration().mnc; 10 str += " IMSI MCC (Mobile Country Code): " + String.valueOf(mcc) + " \n " ; 11 str += " IMSI MNC (Mobile Network Code): " + String.valueOf(mnc) + " \n " ; 12 result = str; 13 return result; 14 }
1 public static String fetch_cpu_info() { 2 String result = null ; 3 CMDExecute cmdexe = new CMDExecute(); 4 try { 5 String[ ] args = { " /system/bin/cat " , " /proc/cpuinfo " }; 6 result = cmdexe.run(args, " /system/bin/ " ); 7 Log.i( " result " , " result= " + result); 8 } catch (IOException ex) { 9 ex.printStackTrace(); 10 } 11 return result; 12 }
1 /** 2 *系统内存情况查看 3 */ 4 public static String getMemoryInfo(Context context) { 5 StringBuffer memoryInfo = new StringBuffer(); 6 7 final ActivityManager activityManager = 8 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 9 ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); 10 activityManager.getMemoryInfo(outInfo); 11 12 memoryInfo.append( " \nTotal Available Memory : " ).append(outInfo.availMem >> 10 ).append( " k " ); 13 memoryInfo.append( " \nTotal Available Memory : " ).append(outInfo.availMem >> 20 ).append( " k " ); 14 memoryInfo.append( " \nIn low memory situation: " ).append(outInfo.lowMemory); 15 16 String result = null ; 17 CMDExecute cmdexe = new CMDExecute(); 18 try { 19 String[ ] args = { " /system/bin/cat " , " /proc/meminfo " }; 20 result = cmdexe.run(args, " /system/bin/ " ); 21 } catch (IOException ex) { 22 Log.i( " fetch_process_info " , " ex= " + ex.toString()); 23 } 24 return (memoryInfo.toString() + " \n\n " + result); 25 }
1.2.3.3 获取磁盘信息
手机设备的磁盘信息可以通过df命令获取,所以,这里获取磁盘信息的方法和前面类似,惟一不同的是,这个是直接执行命令,获取其命令的返回就可以了,关键代码如下:
// 磁盘信息 public static String fetch_disk_info() { String result = null ; CMDExecute cmdexe = new CMDExecute(); try { String[ ] args = { " /system/bin/df " }; result = cmdexe.run(args, " /system/bin/ " ); Log.i( " result " , " result= " + result); } catch (IOException ex) { ex.printStackTrace(); } return result; }
1.2.3.4 获取网络信息
要获取手机设备的网络信息,只要读取/system/bin/netcfg中的信息就可以了,关键代码如下:
public static String fetch_netcfg_info() { String result = null ; CMDExecute cmdexe = new CMDExecute(); try { String[ ] args = { " /system/bin/netcfg " }; result = cmdexe.run(args, " /system/bin/ " ); } catch (IOException ex) { Log.i( " fetch_process_info " , " ex= " + ex.toString()); } return result; }
1.2.3.5获取显示频信息
除了显示手机的CPU、内存、磁盘信息外,还有个非常重要的硬件,显示频。在Android中,它提供了DisplayMetrics类,可以通过getApplication Context()、getResources()、getDisplayMetrics()初始化,进而读取其屏幕宽(widthPixels)、高(heightPixels)等信息,实现的关键代码如下:
1 public static String getDisplayMetrics(Context cx) { 2 String str = "" ; 3 DisplayMetrics dm = new DisplayMetrics(); 4 dm = cx.getApplicationContext().getResources().getDisplayMetrics(); 5 int screenWidth = dm.widthPixels; 6 int screenHeight = dm.heightPixels; 7 float density = dm.density; 8 float xdpi = dm.xdpi; 9 float ydpi = dm.ydpi; 10 str += " The absolute width: " + String.valueOf(screenWidth) + " pixels\n " ; 11 str += " The absolute heightin: " + String.valueOf(screenHeight) + " pixels\n " ; 12 str += " The logical density of the display. : " + String.valueOf(density) + " \n " ; 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 public void onCreate(Bundle icicle) { 2 Super.onCreate(icicle); 3 setContentView(R.layout.softwares); 4 setTitle( " 软件信息 " ); 5 itemlist = (ListView) findViewById(R.id.itemlist); 6 pd = ProgressDialog.show( this , " 请稍候. . " , " 正在收集你已经安装的软件信息. . . " , true , false ); 7 Thread thread = new Thread( this ); 8 thread.start(); 9 }
1 public ArrayList fetch_installed_apps () { 2 List < ApplicationInfo > packages = getPackageManager().getInstalledApplications( 0 ); 3 ArrayList < HashMap < String, Object >> list = new ArrayList < HashMap < String, Object >> (packages.size()); 4 5 Iterator < ApplicationInfo > l = packages.iterator(); 6 while (l.hasNext()) { 7 HashMap < String, Object > map = new HashMap < String, Object > (); 8 ApplicationInfo app = (ApplicationInfo) l.next(); 9 String packageName = app.packageName; 10 String label = " " ; 11 try { 12 label = getPackageManager().getApplicationLabel(app).toString(); 13 } catch (Exception e) { 14 Log.i( " Exception " , e.toString() 15 ); 16 } 17 map = new HashMap < String, Object > (); 18 map.put( " name " , label); 19 map.put( " desc " , packageName); 20 list.add(map); 21 } 22 return list; 23 }
private void refreshListItems() { list = fetch_installed_apps(); SimpleAdapter notes = new SimpleAdater( this , list, R.layout.info_row, new String[] { " name " , " desc " }, new int [] {R.id.name, R.id.desc}); list.setAdapter(notes); setTitle( " 软件信息,已经安装 " + list.size() + " 款应用. " ); }
1.2.5 获取运行时信息
运行时的一些信息,包括后台运行的Service、Task,以及进程信息,其运行界面如图20。
1.2.5.1 获取正在运行的Service信息
可以通过调用context.getSystemService(Context.ACTIVITY_SERVICE)获取ActivityManager,进而通过系统提供的方法getRunningServices(int maxNum)获取正在运行的服务列表(RunningServiceInfo),再对其结果进一步分析,得到服务对应的进程名及其他信息,实现的关键代码如下:
1 // 正在运行的服务列表 2 public static String getRunningServicesInfo(Context context) { 3 StringBuffer serviceInfo = new StringBuffer(); 4 final ActivityManager activityManager = (ActivityManager) context 5 .getSystemService(Context. ACTIVITY_SERVICE); 6 List < RunningServiceInfo > services = activityManager.getRunningServices( 100 ); 7 8 Iterator < RunningServiceInfo > l = services.iterator(); 9 while (l.hasNext()) { 10 RunningServiceInfo si = (RunningServiceInfo) l.next(); 11 serviceInfo.append( " pid: " ).append(si.pid); 12 serviceInfo.append( " \nprocess: " ).append(si. process); 13 serviceInfo.append( " \nservice: " ).append(si. service); 14 serviceInfo.append( " \ncrashCount: " ).append(si. crashCount); 15 serviceInfo.append( " \nclicentCount: " ).append(si.clientCount); 16 serviceInfo.append( " \nactiveSince: " ).append(ToolHelper.formatData(si.activeSince)); 17 serviceInfo.append( " \nlastActivityTime: " ).append(ToolHelper.formatData(si.lastActivityTime)); 18 serviceInfo.append( " \n\n " ); 19 } 20 return serviceInfo.toString(); 21 }
1.2.5.2 获取正在运行的Task信息
获取正在运行的Task信息调用的是activityManager.getRunningTasks(int maxNum)来获取对应的正在运行的任务信息列表(RunningTaskInfo),进而分析、显示任务信息,其关键代码如下:
1 public static String getRunningTaskInfo(Context context) { 2 StringBuffer sInfo = new StringBuffer(); 3 final ActivityManager activityManager = (ActivityManager) context 4 .getSystemService(Context. ACTIVITY_SERVICE); 5 List < RunningTaskInfo > tasks = activityManager.getRunningTasks( 100 ); 6 Iterator < RunningTaskInfo > l = tasks.iterator(); 7 while (l.hasNext()) { 8 RunningTaskInfo ti = (RunningTaskInfo) l.next(); 9 sInfo.append( " id: " ).append(ti.id); 10 sInfo.append( " \nbaseActivity: " ).append(ti. baseActivity.flattenToString()); 11 sInfo.append( " \nnumActivities: " ).append(ti. nnumActivities); 12 sInfo.append( " \nnumRunning: " ).append(ti. numRunning); 13 sInfo.append( " \ndescription: " ).append(ti. description); 14 sInfo.append( " \n\n " ); 15 } 16 return sInfo.toString(); 17 }
1.2.5.3 获取正在运行的进程信息
该段程序是通过CMD Execute的方式来运行系统命令。关键代码如下:
1 public static String fetch_process_info() { 2 Log.i( " fetch_process_info " , " start. . . . " ); 3 String result = null ; 4 CMDExecutr cmdexe = new CMDExecute(); 5 try { 6 String [ ] args = { " /system/bin/top " , " -n " , " 1 " }; 7 result = cmdexe.run(args, " /system/bin/ " ); 8 } catch (IOException ex) { 9 Log.i( " fetch_process_info " , " ex= " + ex.toString()); 10 } 11 return result; 12 }
1 @Override 2 public void onItemClick(AdapterView <?> parent, View v, int position, long id) { 3 Log.i(TAG, " item clicked! [ " + position + " ] " ); 4 if (position == 0 ) { 5 path = " / " ; 6 refreshListItems(path); 7 } else if (position == 1 ) { 8 goToParent(); 9 } else { 10 path = (String) list.get(position).get( " path " ); 11 File file = new File(path); 12 if (file.isDirectory()) 13 refreshListItems(path); 14 else 15 Toast.makeText(FSExplorer. this ,getString(R.string.is_file), Toast.LENGTH_SHORT).show(); 16 } 17 }
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的屬性變量中進行判斷。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。