首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

读取手机硬件软件信息以及流量统计的种

2012-09-22 
读取手机硬件软件信息以及流量统计的类可以读取手机的cpu 内存 键盘类型 导航方式 以及安装的软件列表等附

读取手机硬件软件信息以及流量统计的类
可以读取手机的cpu 内存 键盘类型 导航方式 以及安装的软件列表等
附带流量查询方法
这个方法对手机的负荷较大 可以考虑放在后台执行public PhoneInfoUtil(Activity activity,PhoneInfo pinfo){
this.context=activity.getApplicationContext();
this.wm=activity.getWindowManager();
this.activity=activity;
fetch_info(pinfo);//linux方式获得
fetch_tel_status(pinfo);//android API方式获得


}
/**
* linux版本 cpu信息 mem信息
* @param pInfo
*/
  public void fetch_info(PhoneInfo pInfo) {
    String linuxInfo = null;
    String memInfo = null;
    String cpuInfo = null;
    CMDExecute cmdexe = new CMDExecute ( );
    try {
    String[ ] linuxargs = {"/system/bin/cat", "/proc/version" };
    linuxInfo = cmdexe.run(linuxargs, "system/bin/");
    String[] cpuargs = {"/system/bin/cat", "/proc/cpuinfo"};
    cpuInfo = cmdexe.run(cpuargs, "/system/bin/");
    String[] memargs = {"/system/bin/cat", "/proc/meminfo"};
    memInfo = cmdexe.run(cpuargs, "/system/bin/");
   
    } catch (IOException ex) {
    ex.printStackTrace( );
    }
    pInfo.setLinuxInfo(linuxInfo);
    pInfo.setCpuInfo(cpuInfo);
    pInfo.setMemInfo(memInfo);
    }
   
    public void fetch_tel_status(PhoneInfo pInfo) {
    String result = null;
    String networktype = null;
    String phoneType = null;
    String keyType = null;
    String navigationType = null;
   
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);//
    String str = " ";
    //串号IMEI
    pInfo.setIMEI(tm.getDeviceId());
    //串号版本
    pInfo.setIMEI_ver(tm.getDeviceSoftwareVersion());
    //联网类型
    switch(tm.getNetworkType()){
    case TelephonyManager.NETWORK_TYPE_EDGE:
    networktype="EDGE";break;
    case TelephonyManager.NETWORK_TYPE_GPRS:
    networktype="GPRS";
    case TelephonyManager.NETWORK_TYPE_UMTS:
    networktype="UMTS";
    default :
    networktype="UNKNOWN";break;
    }
    pInfo.setNetworktype(networktype);
    //手机网络类型
    switch(tm.getPhoneType()){
    case TelephonyManager.PHONE_TYPE_GSM:
    phoneType="GSM";break;
    case TelephonyManager.PHONE_TYPE_NONE:
    phoneType="NONE";break;
    default:
    phoneType="UNKNOWN";break;
    }
    pInfo.setPhoneType(phoneType);
   
    //键盘类型
    switch(context.getResources().getConfiguration().keyboard){
    case 0:
    keyType="KEYBOARD_UNDEFINED";break;
    case 1:
    keyType="KEYBOARD_NOKEYS";break;
    case 2:
    keyType="KEYBOARD_QWERTY";break;
    case 3:
    keyType="KEYBOARD_12KEY";break;
    }
    pInfo.setKeyType(keyType);
    //导航方式
    switch(context.getResources().getConfiguration().navigation){
    case 0:
    navigationType="NAVIGATION_UNDEFINED";break;
    case 1:
    navigationType="NAVIGATION_NONAV";break;
    case 2:
    navigationType="NAVIGATION_DPAD";break;
    case 3:
    navigationType="NAVIGATION_TRACKBALL";break;
    }
    pInfo.setNavigationType(navigationType);
    //系统版本
    int version = 0;
    try{
    version = Integer.valueOf(android.os.Build.VERSION.SDK);
    }catch(NumberFormatException e){
    e.printStackTrace();
    }
    switch(version){
    case 1:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"1.0");break;
    case 2:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"1.1");break;
    case 3:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"1.5");break;
    case 4:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"1.6");break;
    case 5:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.0");break;
    case 6:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.0.1");break;
    case 7:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.1");break;
    case 8:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.2");break;
    case 9:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.3");break;
    case 10:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"2.3.3");break;
    case 11:
    pInfo.setSystemInfo(android.os.Build.MODEL+" : "+"3.0");break;
    }
    //屏幕信息 包括密度 分辨率
    DisplayMetrics dm=new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    String screenInfo="density = "+dm.density;
    screenInfo+=" heightPixels = "+dm.heightPixels;
    screenInfo+=" widthPixels = "+dm.widthPixels;
    pInfo.setScreenInfo(screenInfo);
    //获取手机内安装的应用列表
    String appsInfo="";
    for(int i=0;i<activity.getPackageManager().getInstalledApplications(0).size();i++){
    ApplicationInfo info=(ApplicationInfo) activity.getPackageManager().getInstalledApplications(0).get(i);
    if(info.packageName.equals("com.android")||info.packageName.equals("com.android")){
   
    }
    else{
    appsInfo+=info.className+",";
    }
    }
    pInfo.setAppsInfo(appsInfo);
    }
    /**
     * linux命令的执行封装类
     * @author chaos
     *
     */
    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;   
          }
        }
    /**
     * 统计GPRS流量
     * 注1:还可以统计wifi usb流量注2:关机后该文件内容清空
     * @return
     */
public long countGPRS()
{
FileReader fstream = null;
try {
fstream = new FileReader(DEV_FILE);
}
catch (FileNotFoundException e) {
}
BufferedReader in = new BufferedReader(fstream, 500);
String line;
String[] segs;
String[] netdata;
int k;
int j;
try {
while ((line = in.readLine()) != null) {
segs = line.trim().split(":");
if (line.startsWith(GPRSLINE))
{
netdata = segs[1].trim().split(" ");
for (k = 0, j = 0; k < netdata.length; k++)
{
if (netdata[k].length() > 0)
{
gprsdata[j] = netdata[k];
j++;
}
}
}
}
fstream.close();
}
catch (IOException e) {
}
long result = +Long.parseLong(gprsdata[0])+Long.parseLong(gprsdata[1])+Long.parseLong(gprsdata[8])+Long.parseLong(gprsdata[9]);
return result;
}

}

热点排行