轉自:http://gqdy365.iteye.com/blog/1066113
一、記憶體(ram):
android的總記憶體大小資訊存放在系統的/proc/meminfo檔裡面,可以通過讀取這個檔來獲取這些資訊:

 

Java代碼
1.public void getTotalMemory() {
2. String str1 = "/proc/meminfo";
3. String str2="";
4. try {
5. FileReader fr = new FileReader(str1);
6. BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
7. while ((str2 = localBufferedReader.readLine()) != null) {
8. Log.i(TAG, "---" + str2);
9. }
10. } catch (IOException e) {
11. }
12. }


 

運行資訊如下:

 

Java代碼
1.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemTotal: 204876 kB
2.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree: 4596 kB
3.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers: 16020 kB
4.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached: 82508 kB
5.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached: 64 kB
6.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active: 137104 kB
7.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive: 41056 kB
8.05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB
9.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB
10.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty: 88 kB
11.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback: 0 kB
12.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB
13.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped: 38296 kB
14.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab: 5768 kB
15.05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB
16.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB
17.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables: 8184 kB
18.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB
19.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce: 0 kB
20.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB
21.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB
22.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB
23.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB
24.05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocChunk: 674820 kB

 

第一行是總記憶體大小(即使用者可以使用的ram的大小)!其他各項的介紹大家可以看這兒:
http://bg135.com/android-phones-to-get-the-total-memory-and-available-memory.html

 

獲取當前剩餘記憶體(ram)大小的方法:

 

Java代碼
1.public long getAvailMemory() {
2. ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
3. ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
4. am.getMemoryInfo(mi);
5. return mi.availMem;
6. }


 

二、Rom大小

 

Java代碼
1.public long[] getRomMemroy() {
2. long[] romInfo = new long[2];
3. //Total rom memory
4. romInfo[0] = getTotalInternalMemorySize();
5.
6. //Available rom memory
7. File path = Environment.getDataDirectory();
8. StatFs stat = new StatFs(path.getPath());
9. long blockSize = stat.getBlockSize();
10. long availableBlocks = stat.getAvailableBlocks();
11. romInfo[1] = blockSize * availableBlocks;
12. getVersion();
13. return romInfo;
14. }
15.
16. public long getTotalInternalMemorySize() {
17. File path = Environment.getDataDirectory();
18. StatFs stat = new StatFs(path.getPath());
19. long blockSize = stat.getBlockSize();
20. long totalBlocks = stat.getBlockCount();
21. return totalBlocks * blockSize;
22. }

 

注意類型,不然相乘之後會有溢出。可用內部存儲的大小不能通過getRootDirectory();
取得,之前網上傳的很多都是用getRootDirectory()取得的,我測試之後發現取得的數值不對。要根據getDataDirectory();
取得。
三、sdCard大小
 
Java代碼
1.public long[] getSDCardMemory() {
2. long[] sdCardInfo=new long[2];
3. String state = Environment.getExternalStorageState();
4. if (Environment.MEDIA_MOUNTED.equals(state)) {
5. File sdcardDir = Environment.getExternalStorageDirectory();
6. StatFs sf = new StatFs(sdcardDir.getPath());
7. long bSize = sf.getBlockSize();
8. long bCount = sf.getBlockCount();
9. long availBlocks = sf.getAvailableBlocks();
10.
11. sdCardInfo[0] = bSize * bCount;//總大小
12. sdCardInfo[1] = bSize * availBlocks;//可用大小
13. }
14. return sdCardInfo;
15. }
 
注意類型,不然相乘之後會有溢出。
 
四、電池電量
 
Java代碼
1.private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
2. @Override
3. public void onReceive(Context context, Intent intent) {
4. int level = intent.getIntExtra("level", 0);
5. // level加%就是當前電量了
6. }
7. };
 
然後在activity的oncreate()方法中註冊
 
Java代碼
1.registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
 
五、CPU資訊
 
Java代碼
1.public String[] getCpuInfo() {
2. String str1 = "/proc/cpuinfo";
3. String str2="";
4. String[] cpuInfo={"",""};
5. String[] arrayOfString;
6. try {
7. FileReader fr = new FileReader(str1);
8. BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
9. str2 = localBufferedReader.readLine();
10. arrayOfString = str2.split("\\s+");
11. for (int i = 2; i < arrayOfString.length; i++) {
12. cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
13. }
14. str2 = localBufferedReader.readLine();
15. arrayOfString = str2.split("\\s+");
16. cpuInfo[1] += arrayOfString[2];
17. localBufferedReader.close();
18. } catch (IOException e) {
19. }
20. return cpuInfo;
21.}
 
/proc/cpuinfo檔中第一行是CPU的型號,第二行是CPU的頻率,可以通過讀檔,讀取這些資料!
 
六、系統的版本資訊:
 
Java代碼
1.public String[] getVersion(){
2. String[] version={"null","null","null","null"};
3. String str1 = "/proc/version";
4. String str2;
5. String[] arrayOfString;
6. try {
7. FileReader localFileReader = new FileReader(str1);
8. BufferedReader localBufferedReader = new BufferedReader(
9. localFileReader, 8192);
10. str2 = localBufferedReader.readLine();
11. arrayOfString = str2.split("\\s+");
12. version[0]=arrayOfString[2];//KernelVersion
13. localBufferedReader.close();
14. } catch (IOException e) {
15. }
16. version[1] = Build.VERSION.RELEASE;// firmware version
17. version[2]=Build.MODEL;//model
18. version[3]=Build.DISPLAY;//system version
19. return version;
20.}
 
版本資訊裡面還包括型號等資訊。
 
七、MAC位址和開機時間:
 
Java代碼
1.public String[] getOtherInfo(){
2. String[] other={"null","null"};
3. WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
4. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
5. if(wifiInfo.getMacAddress()!=null){
6. other[0]=wifiInfo.getMacAddress();
7. } else {
8. other[0] = "Fail";
9. }
10. other[1] = getTimes();
11. return other;
12.}
13.private String getTimes() {
14. long ut = SystemClock.elapsedRealtime() / 1000;
15. if (ut == 0) {
16. ut = 1;
17. }
18. int m = (int) ((ut / 60) % 60);
19. int h = (int) ((ut / 3600));
20. return h + " " + mContext.getString(R.string.info_times_hour) + m + " "
21. + mContext.getString(R.string.info_times_minute);
22.}
 
最後貼一個格式化資料的方法:
 
Java代碼
1.public String formatSize(long size) {
2. String suffix = null;
3. float fSize=0;
4.
5. if (size >= 1024) {
6. suffix = "KB";
7. fSize=size / 1024;
8. if (fSize >= 1024) {
9. suffix = "MB";
10. fSize /= 1024;
11. }
12. if (fSize >= 1024) {
13. suffix = "GB";
14. fSize /= 1024;
15. }
16. } else {
17. fSize = size;
18. }
19. java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
20. StringBuilder resultBuffer = new StringBuilder(df.format(fSize));
21. if (suffix != null)
22. resultBuffer.append(suffix);
23. return resultBuffer.toString();
24.}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()