zoukankan      html  css  js  c++  java
  • android---获取设备相关信息

      1 public class DevManager
      2 {
      3     private static final String TAG = "CpuManager";
      4     Context mContext;
      5 
      6     public DevManager()
      7     {
      8 
      9     }
     10 
     11     public DevManager(Context context)
     12     {
     13         mContext = context;
     14     }
     15 
     16     // 获取CPU最大频率(单位KHZ)
     17     // "/system/bin/cat" 命令行
     18     // "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
     19     public static String getMaxCpuFreq()
     20     {
     21         String result = "";
     22         ProcessBuilder cmd;
     23         try
     24         {
     25             String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
     26             cmd = new ProcessBuilder(args);
     27             Process process = cmd.start();
     28             InputStream in = process.getInputStream();
     29             byte[] re = new byte[24];
     30             while (in.read(re) != -1)
     31             {
     32                 result = result + new String(re);
     33             }
     34             in.close();
     35         } catch (IOException ex)
     36         {
     37             ex.printStackTrace();
     38             result = "N/A";
     39         }
     40         return result.trim();
     41     }
     42 
     43     // 获取CPU最小频率(单位KHZ)
     44     public static String getMinCpuFreq()
     45     {
     46         String result = "";
     47         ProcessBuilder cmd;
     48         try
     49         {
     50             String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
     51             cmd = new ProcessBuilder(args);
     52             Process process = cmd.start();
     53             InputStream in = process.getInputStream();
     54             byte[] re = new byte[24];
     55             while (in.read(re) != -1)
     56             {
     57                 result = result + new String(re);
     58             }
     59             in.close();
     60         } catch (IOException ex)
     61         {
     62             ex.printStackTrace();
     63             result = "N/A";
     64         }
     65         return result.trim();
     66     }
     67 
     68     // 实时获取CPU当前频率(单位KHZ)
     69     public static String getCurCpuFreq()
     70     {
     71         String result = "N/A";
     72         try
     73         {
     74             FileReader fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
     75             BufferedReader br = new BufferedReader(fr);
     76             String text = br.readLine();
     77             result = text.trim();
     78         } catch (FileNotFoundException e)
     79         {
     80             e.printStackTrace();
     81         } catch (IOException e)
     82         {
     83             e.printStackTrace();
     84         }
     85         return result;
     86     }
     87 
     88     // 获取CPU名字
     89     public static String getCpuName()
     90     {
     91         try
     92         {
     93             FileReader fr = new FileReader("/proc/cpuinfo");
     94             BufferedReader br = new BufferedReader(fr);
     95             String text = br.readLine();
     96             String[] array = text.split(":\s+", 2);
     97             for (int i = 0; i < array.length; i++)
     98             {
     99             }
    100             return array[1];
    101         } catch (FileNotFoundException e)
    102         {
    103             e.printStackTrace();
    104         } catch (IOException e)
    105         {
    106             e.printStackTrace();
    107         }
    108         return null;
    109     }
    110 
    111     // 内存
    112     public void getTotalMemory()
    113     {
    114         String str1 = "/proc/meminfo";
    115         String str2 = "";
    116         try
    117         {
    118             FileReader fr = new FileReader(str1);
    119             BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
    120             while ((str2 = localBufferedReader.readLine()) != null)
    121             {
    122                 Log.i(TAG, "---" + str2);
    123             }
    124         } catch (IOException e)
    125         {
    126         }
    127     }
    128 
    129     // rom大小
    130     public long[] getRomMemroy()
    131     {
    132         long[] romInfo = new long[2];
    133         // Total rom memory
    134         romInfo[0] = getTotalInternalMemorySize();
    135 
    136         // Available rom memory
    137         File path = Environment.getDataDirectory();
    138         StatFs stat = new StatFs(path.getPath());
    139         long blockSize = stat.getBlockSize();
    140         long availableBlocks = stat.getAvailableBlocks();
    141         romInfo[1] = blockSize * availableBlocks;
    142         getVersion();
    143         return romInfo;
    144     }
    145 
    146     public long getTotalInternalMemorySize()
    147     {
    148         File path = Environment.getDataDirectory();
    149         StatFs stat = new StatFs(path.getPath());
    150         long blockSize = stat.getBlockSize();
    151         long totalBlocks = stat.getBlockCount();
    152         return totalBlocks * blockSize;
    153     }
    154 
    155     // sd卡大小
    156     public long[] getSDCardMemory()
    157     {
    158         long[] sdCardInfo = new long[2];
    159         String state = Environment.getExternalStorageState();
    160         if (Environment.MEDIA_MOUNTED.equals(state))
    161         {
    162             File sdcardDir = Environment.getExternalStorageDirectory();
    163             StatFs sf = new StatFs(sdcardDir.getPath());
    164             long bSize = sf.getBlockSize();
    165             long bCount = sf.getBlockCount();
    166             long availBlocks = sf.getAvailableBlocks();
    167 
    168             sdCardInfo[0] = bSize * bCount;// 总大小
    169             sdCardInfo[1] = bSize * availBlocks;// 可用大小
    170         }
    171         return sdCardInfo;
    172     }
    173 
    174     // 电池容量
    175     private BroadcastReceiver batteryReceiver = new BroadcastReceiver()
    176     {
    177 
    178         @Override
    179         public void onReceive(Context context, Intent intent)
    180         {
    181             int level = intent.getIntExtra("level", 0);
    182             // level加%就是当前电量了
    183         }
    184     };
    185 
    186 //    registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    187 
    188     // 系统版本信息
    189     public String[] getVersion()
    190     {
    191         String[] version = { "null", "null", "null", "null" };
    192         String str1 = "/proc/version";
    193         String str2;
    194         String[] arrayOfString;
    195         try
    196         {
    197             FileReader localFileReader = new FileReader(str1);
    198             BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
    199             str2 = localBufferedReader.readLine();
    200             arrayOfString = str2.split("\s+");
    201             version[0] = arrayOfString[2];// KernelVersion
    202             localBufferedReader.close();
    203         } catch (IOException e)
    204         {
    205         }
    206         version[1] = Build.VERSION.RELEASE;// firmware version
    207         version[2] = Build.MODEL;// model
    208         version[3] = Build.DISPLAY;// system version
    209         return version;
    210     }
    211 
    212     // mac地址和开机时间
    213     public String[] getOtherInfo()
    214     {
    215         String[] other = { "null", "null" };
    216         WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    217         WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    218         if (wifiInfo.getMacAddress() != null)
    219         {
    220             other[0] = wifiInfo.getMacAddress();
    221         }
    222         else
    223         {
    224             other[0] = "Fail";
    225         }
    226         other[1] = getTimes();
    227         return other;
    228     }
    229 
    230     private String getTimes()
    231     {
    232         long ut = SystemClock.elapsedRealtime() / 1000;
    233         if (ut == 0)
    234         {
    235             ut = 1;
    236         }
    237         int m = (int) ((ut / 60) % 60);
    238         int h = (int) ((ut / 3600));
    239 //        return h + " " + mContext.getString(R.string.info_times_hour) + m + " " + mContext.getString(R.string.info_times_minute);
    240         return h + " " + "时" + m + " " + "分";
    241     }
    242 }
  • 相关阅读:
    Javascript继承,再谈
    RHEL7使用systemctl管理服务
    centos7进入单用户模式修改root密码
    IBM DS5020 管理口密码重置
    IBM小机拆镜像换盘
    HMC版本支持
    IBM产品系列和AIX系统版本
    AIX 6.1创建逻辑卷并挂载【smitty】
    AIX中的网络管理
    创建AIX克隆盘
  • 原文地址:https://www.cnblogs.com/garfieldx/p/3408817.html
Copyright © 2011-2022 走看看