android擷取手機資訊
android系統的很多資訊可以通過 /proc 目錄下獲得,如
cat /proc/cpuinfo 擷取cpu資訊
cat /proc/meminfo 擷取記憶體資訊
這些資訊以文本格式儲存,可以通過IO流讀取,比較簡單,在這裡考慮到一些內容並不是以文本方式儲存,磁碟資訊
我們通過代碼實現一個linux指令解析器來得到要擷取的資訊
指令解析器如下:
public class CMDExecutor {/** * 執行命令 * @param cmd 命令參數 * @param workdir 目前的目錄(即執行指令 pwd 看到所在目錄) * @return */public synchronized String run(String[] cmd, String workdir){String result = "";InputStream is = null;try {ProcessBuilder builder = new ProcessBuilder(cmd);if(!TextUtils.isEmpty(workdir)){builder.directory(new File(workdir)); // 設定執行指令執所在目錄builder.redirectErrorStream();Process process = builder.start(); // 執行指令is = process.getInputStream();byte[] buf = new byte[1024];int len = 0;while((len = is.read(buf)) != -1){result += new String(buf, 0, len);}}} catch (Exception e) {return null;}finally{if(is != null){try {is.close();} catch (IOException e) {e.printStackTrace();}}}return result;}}
獲致手機資訊工具類:
public class GetPhoneInfoUtil {// 擷取核心版本,gcc版本等public String fetch_version_info(){String result = null;CMDExecutor cmdexe = new CMDExecutor();String[] args = {"/system/bin/cat", "/proc/version"}; // "/system/bin/cat" 指令絕對路徑result = cmdexe.run(args, "system/bin/"); // 已指定指令執行目錄,上面絕對路徑可寫相對路徑 "cat"return result;}// 擷取CPU資訊public String fetch_cpu_info() {String result = null;CMDExecutor cmdexe = new CMDExecutor();String[] args = { "/system/bin/cat", "/proc/cpuinfo" };result = cmdexe.run(args, "/system/bin/");return result;}// 要加輸許可權:android.permission.READ_PHONE_STATE// 電訊廠商資訊public String fetch_tel_status(Context context) {String result = null;TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);String str = " ";str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";int mcc = context.getResources().getConfiguration().mcc;int mnc = context.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;}private static StringBuffer buffer = null;public static String initProperty(String description, String propertyStr) {if (buffer == null) {buffer = new StringBuffer();}buffer.append(description).append(":\t");buffer.append(System.getProperty(propertyStr)).append("\n");return buffer.toString();}public static String getSystemProperty() {buffer = new StringBuffer();initProperty("java.vendor.url", "java.vendor.url");initProperty("java.class.path", "java.class.path");initProperty("os.name", "os.name");initProperty("os.version", "os.version");initProperty("user.home", "user.home");return buffer.toString();}// 擷取網路資訊public static String fetch_netcfg_info(){String result = null;CMDExecutor cmdexe = new CMDExecutor();String[] args = { "/system/bin/netcfg" };result = cmdexe.run(args, "/system/bin/");return result;}// 磁碟資訊public static String fetch_disk_info() {String result = null;CMDExecutor cmdexe = new CMDExecutor();String[] args = { "/system/bin/df" };result = cmdexe.run(args, "/system/bin/");return result;}// 擷取顯示頻資訊public static String getDisplayMetrics(Context context) {String str = "";DisplayMetrics dm = new DisplayMetrics();dm = context.getResources().getDisplayMetrics();// 上面一行等同於下面兩行//WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);//wm.getDefaultDisplay().getMetrics(dm);int screenWidth = dm.widthPixels;int screenHeight = dm.heightPixels;float density = dm.density;float xdpi = dm.xdpi;float ydpi = dm.ydpi;str += "The absolute width: " + String.valueOf(screenWidth) + "pixels \n";str += "The absolute heightin: " + String.valueOf(screenHeight)+ "pixels \n";str += "The logical density of the display :"+ String.valueOf(density) + " \n";str += "X dimension : " + String.valueOf(xdpi) + "pixels per inch \n";str += "Y dimension : " + String.valueOf(ydpi) + "pixels per inch \n";return str;}}
關於System.getProperty(),可參考System.getProperty()方法可以擷取的值