【Android】获取手机的相关硬件信息

今天在QQ群里聊天,一个哥们在某宝买到了一个运行内存16G的手机,当时我就吓尿了,所以有了写个程序把这个手机的实际内存读出来的想法,于是就有了今天这篇博客.


所有的信息项如下图所示.(由于我的测试机没有插手机卡,所以有的信息会显示为空)



以下就是代码:

[java]  view plain copy
  1. package com.liu.chad.practicesqlite;  
  2.   
  3. import android.app.ActivityManager;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.telephony.TelephonyManager;  
  8. import android.text.format.Formatter;  
  9. import android.util.Log;  
  10. import android.widget.TextView;  
  11.   
  12. import java.io.BufferedReader;  
  13. import java.io.FileNotFoundException;  
  14. import java.io.FileReader;  
  15. import java.io.IOException;  
  16. import java.io.InputStream;  
  17.   
  18. public class MainActivity extends ActionBarActivity {  
  19.   
  20.     private TextView mTextView;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.   
  27.         mTextView = (TextView) findViewById(R.id.textViewId);  
  28.         getPhoneInfo();  
  29.     }  
  30.   
  31.     /** 
  32.      * 获取手机信息 
  33.      */  
  34.     public void getPhoneInfo() {  
  35.         TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);  
  36.         String mtyb = android.os.Build.BRAND;// 手机品牌  
  37.         String mtype = android.os.Build.MODEL; // 手机型号  
  38.         String imei = tm.getDeviceId();  
  39.         String imsi = tm.getSubscriberId();  
  40.         String numer = tm.getLine1Number(); // 手机号码  
  41.         String serviceName = tm.getSimOperatorName(); // 运营商  
  42.         mTextView.setText("品牌: " + mtyb + "\n" + "型号: " + mtype + "\n" + "版本: Android "  
  43.                 + android.os.Build.VERSION.RELEASE + "\n" + "IMEI: " + imei  
  44.                 + "\n" + "IMSI: " + imsi + "\n" + "手机号码: " + numer + "\n"  
  45.                 + "运营商: " + serviceName + "\n");  
  46.         mTextView.append("总内存: " + getTotalMemory() + "\n");  
  47.         mTextView.append("当前可用内存: " + getAvailMemory() + "\n");  
  48.         mTextView.append("CPU名字: " + getCpuName() + "\n");  
  49.         mTextView.append("CPU最大频率: " + getMaxCpuFreq() + "\n");  
  50.         mTextView.append("CPU最小频率: " + getMinCpuFreq() + "\n");  
  51.         mTextView.append("CPU当前频率: " + getCurCpuFreq() + "\n");  
  52.     }  
  53.   
  54.     /** 
  55.      * 获取手机内存大小 
  56.      * 
  57.      * @return 
  58.      */  
  59.     private String getTotalMemory() {  
  60.         String str1 = "/proc/meminfo";// 系统内存信息文件  
  61.         String str2;  
  62.         String[] arrayOfString;  
  63.         long initial_memory = 0;  
  64.         try {  
  65.             FileReader localFileReader = new FileReader(str1);  
  66.             BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);  
  67.             str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小  
  68.   
  69.             arrayOfString = str2.split("\\s+");  
  70.             for (String num : arrayOfString) {  
  71.                 Log.i(str2, num + "\t");  
  72.             }  
  73.   
  74.             initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte  
  75.             localBufferedReader.close();  
  76.   
  77.         } catch (IOException e) {  
  78.         }  
  79.         return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化  
  80.     }  
  81.   
  82.     /** 
  83.      * 获取当前可用内存大小 
  84.      * 
  85.      * @return 
  86.      */  
  87.     private String getAvailMemory() {  
  88.         ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
  89.         ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();  
  90.         am.getMemoryInfo(mi);  
  91.         return Formatter.formatFileSize(getBaseContext(), mi.availMem);  
  92.     }  
  93.   
  94.     public static String getMaxCpuFreq() {  
  95.         String result = "";  
  96.         ProcessBuilder cmd;  
  97.         try {  
  98.             String[] args = {"/system/bin/cat",  
  99.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"};  
  100.             cmd = new ProcessBuilder(args);  
  101.             Process process = cmd.start();  
  102.             InputStream in = process.getInputStream();  
  103.             byte[] re = new byte[24];  
  104.             while (in.read(re) != -1) {  
  105.                 result = result + new String(re);  
  106.             }  
  107.             in.close();  
  108.         } catch (IOException ex) {  
  109.             ex.printStackTrace();  
  110.             result = "N/A";  
  111.         }  
  112.         return result.trim() + "Hz";  
  113.     }  
  114.   
  115.     // 获取CPU最小频率(单位KHZ)  
  116.   
  117.     public static String getMinCpuFreq() {  
  118.         String result = "";  
  119.         ProcessBuilder cmd;  
  120.         try {  
  121.             String[] args = {"/system/bin/cat",  
  122.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"};  
  123.             cmd = new ProcessBuilder(args);  
  124.             Process process = cmd.start();  
  125.             InputStream in = process.getInputStream();  
  126.             byte[] re = new byte[24];  
  127.             while (in.read(re) != -1) {  
  128.                 result = result + new String(re);  
  129.             }  
  130.             in.close();  
  131.         } catch (IOException ex) {  
  132.             ex.printStackTrace();  
  133.             result = "N/A";  
  134.         }  
  135.         return result.trim() + "Hz";  
  136.     }  
  137.   
  138.     // 实时获取CPU当前频率(单位KHZ)  
  139.   
  140.     public static String getCurCpuFreq() {  
  141.         String result = "N/A";  
  142.         try {  
  143.             FileReader fr = new FileReader(  
  144.                     "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");  
  145.             BufferedReader br = new BufferedReader(fr);  
  146.             String text = br.readLine();  
  147.             result = text.trim() + "Hz";  
  148.         } catch (FileNotFoundException e) {  
  149.             e.printStackTrace();  
  150.         } catch (IOException e) {  
  151.             e.printStackTrace();  
  152.         }  
  153.         return result;  
  154.     }  
  155.   
  156.     public static String getCpuName() {  
  157.         try {  
  158.             FileReader fr = new FileReader("/proc/cpuinfo");  
  159.             BufferedReader br = new BufferedReader(fr);  
  160.             String text = br.readLine();  
  161.             String[] array = text.split(":\\s+"2);  
  162.             for (int i = 0; i < array.length; i++) {  
  163.             }  
  164.             return array[1];  
  165.         } catch (FileNotFoundException e) {  
  166.             e.printStackTrace();  
  167.         } catch (IOException e) {  
  168.             e.printStackTrace();  
  169.         }  
  170.         return null;  
  171.     }  
  172. }  

布局文件就是一个TextView,我就不在这里贴出来了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值