Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 獲取手機信息

Android 獲取手機信息

編輯:關於Android編程

[java]   /**       * 獲取手機信息       */       public void getPhoneInfo()       {           TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);           String mtyb = android.os.Build.BRAND;// 手機品牌            String mtype = android.os.Build.MODEL; // 手機型號            String imei = tm.getDeviceId();           String imsi = tm.getSubscriberId();           String numer = tm.getLine1Number(); // 手機號碼            String serviceName = tm.getSimOperatorName(); // 運營商            tvPhoneInfo.setText("品牌: " + mtyb + "\n" + "型號: " + mtype + "\n" + "版本: Android " + android.os.Build.VERSION.RELEASE + "\n" + "IMEI: " + imei                   + "\n" + "IMSI: " + imsi + "\n" + "手機號碼: " + numer + "\n" + "運營商: " + serviceName + "\n");       }     /** * 獲取手機信息 */ public void getPhoneInfo() { TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); String mtyb = android.os.Build.BRAND;// 手機品牌 String mtype = android.os.Build.MODEL; // 手機型號 String imei = tm.getDeviceId(); String imsi = tm.getSubscriberId(); String numer = tm.getLine1Number(); // 手機號碼 String serviceName = tm.getSimOperatorName(); // 運營商 tvPhoneInfo.setText("品牌: " + mtyb + "\n" + "型號: " + mtype + "\n" + "版本: Android " + android.os.Build.VERSION.RELEASE + "\n" + "IMEI: " + imei + "\n" + "IMSI: " + imsi + "\n" + "手機號碼: " + numer + "\n" + "運營商: " + serviceName + "\n"); }       [java]   /**       * 獲取手機內存大小       *        * @return       */       private String getTotalMemory()       {           String str1 = "/proc/meminfo";// 系統內存信息文件            String str2;           String[] arrayOfString;           long initial_memory = 0;           try           {               FileReader localFileReader = new FileReader(str1);               BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);               str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統總內存大小                   arrayOfString = str2.split("\\s+");               for (String num : arrayOfString)               {                   Log.i(str2, num + "\t");               }                  initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總內存,單位是KB,乘以1024轉換為Byte                localBufferedReader.close();              }           catch (IOException e)           {           }           return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉換為KB或者MB,內存大小規格化        }     /** * 獲取手機內存大小 *  * @return */ private String getTotalMemory() { String str1 = "/proc/meminfo";// 系統內存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統總內存大小   arrayOfString = str2.split("\\s+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); }   initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總內存,單位是KB,乘以1024轉換為Byte localBufferedReader.close();   } catch (IOException e) { } return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉換為KB或者MB,內存大小規格化 } [java]  /**   * 獲取當前可用內存大小   *    * @return   */   private String getAvailMemory()   {       ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);       MemoryInfo mi = new MemoryInfo();       am.getMemoryInfo(mi);       return Formatter.formatFileSize(getBaseContext(), mi.availMem);   }     /** * 獲取當前可用內存大小 *  * @return */ private String getAvailMemory() { ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(getBaseContext(), mi.availMem); }         [java]   /**       * 獲取手機CPU信息       *        * @return       */       public String[] getCpuInfo()       {           String str1 = "/proc/cpuinfo";           String str2 = "";           String[] cpuInfo = { "", "" };           String[] arrayOfString;           try           {               FileReader fr = new FileReader(str1);               BufferedReader localBufferedReader = new BufferedReader(fr, 8192);               str2 = localBufferedReader.readLine();               arrayOfString = str2.split("\\s+");               for (int i = 2; i < arrayOfString.length; i++)               {                   cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";               }               str2 = localBufferedReader.readLine();               arrayOfString = str2.split("\\s+");               cpuInfo[1] += arrayOfString[2];               localBufferedReader.close();           }           catch (IOException e)           {           }           tvHardwareInfo.append("CPU型號 " + cpuInfo[0] + "\n" + "CPU頻率: " + cpuInfo[1] + "\n");           return cpuInfo;       }     /** * 獲取手機CPU信息 *  * @return */ public String[] getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = { "", "" }; String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } tvHardwareInfo.append("CPU型號 " + cpuInfo[0] + "\n" + "CPU頻率: " + cpuInfo[1] + "\n"); return cpuInfo; }         [java]   /**   * 獲取CPU核心數   *    * @return   */   private int getNumCores()   {       // Private Class to display only CPU devices in the directory listing        class CpuFilter implements FileFilter       {           @Override           public boolean accept(File pathname)           {               // Check if filename is "cpu", followed by a single digit number                if (Pattern.matches("cpu[0-9]", pathname.getName()))               {                   return true;               }               return false;           }       }          try       {           // Get directory containing CPU info            File dir = new File("/sys/devices/system/cpu/");           // Filter to only list the devices we care about            File[] files = dir.listFiles(new CpuFilter());           // Return the number of cores (virtual CPU devices)            return files.length;       }       catch (Exception e)       {           e.printStackTrace();           // Default to return 1 core            return 1;       }   }     /** * 獲取CPU核心數 *  * @return */ private int getNumCores() { // Private Class to display only CPU devices in the directory listing class CpuFilter implements FileFilter { @Override public boolean accept(File pathname) { // Check if filename is "cpu", followed by a single digit number if (Pattern.matches("cpu[0-9]", pathname.getName())) { return true; } return false; } }   try { // Get directory containing CPU info File dir = new File("/sys/devices/system/cpu/"); // Filter to only list the devices we care about File[] files = dir.listFiles(new CpuFilter()); // Return the number of cores (virtual CPU devices) return files.length; } catch (Exception e) { e.printStackTrace(); // Default to return 1 core return 1; } }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved