Android客制化------在设置中加入RAM flash计算

单纯来看这个其实比较简单的,我们只需要获取对应路径下的信息,同时对其计算与判断,让其显示到对应控件上即可,查看源码,依样画葫芦即可。

diff --git a/res/xml/device_info_status.xml b/res/xml/device_info_status.xml
index fc6016f..81683fd 100644
--- a/res/xml/device_info_status.xml
+++ b/res/xml/device_info_status.xml
@@ -17,6 +17,35 @@
 "http://schemas.android.com/apk/res/android"
         android:title="@string/device_status_activity_title">

+   
+   "add_cpu"
+        style="?android:preferenceInformationStyle"
+        android:title="CPU"
+        android:summary="CPU型号"/>
+
+       "add_ram"
+        style="?android:preferenceInformationStyle"
+        android:title="RAM"
+        android:summary="0G"
+        android:persistent="false"/>
+
+       "add_flash"
+        style="?android:preferenceInformationStyle"
+        android:title="FLASH"
+        android:summary="0G"
+    android:persistent="false"/>
+   

        "battery_status"
         style="?android:attr/preferenceInformationStyle"
         android:title="@string/battery_status_title"
diff --git a/src/com/android/settings/deviceinfo/Status.java b/src/com/android/settings/deviceinfo/Status.java
index b52a0ad..65fcae1 100644
--- a/src/com/android/settings/deviceinfo/Status.java
+++ b/src/com/android/settings/deviceinfo/Status.java
@@ -57,7 +57,20 @@ import java.lang.ref.WeakReference;
  * # XMPP/buzz/tickle status : TODO
  *
  */
-public class Status extends InstrumentedPreferenceActivity {
+import android.widget.Toast; 
+import android.os.Environment;
+import android.os.StatFs;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.text.DecimalFormat;
+
+
+
+public class Status extends PreferenceActivity {

     private static final String KEY_BATTERY_STATUS = "battery_status";
     private static final String KEY_BATTERY_LEVEL = "battery_level";
@@ -97,6 +110,14 @@ public class Status extends InstrumentedPreferenceActivity {
     private Preference mWifiMacAddress;
     private Preference mWimaxMacAddress;


+    private static final String KEY_RAM = "add_ram";
+    private static final String KEY_FLASH = "add_flash";
+    private Preference mAddRAM;
+    private Preference mAddFlash;
+
     private Handler mHandler;

     private static class MyHandler extends Handler {
@@ -174,6 +195,23 @@ public class Status extends InstrumentedPreferenceActivity {
         mWimaxMacAddress = findPreference(KEY_WIMAX_MAC_ADDRESS);
         mIpAddress = findPreference(KEY_IP_ADDRESS);

+
+       mAddRAM=findPreference(KEY_RAM);
+       mAddFlash=findPreference(KEY_FLASH);        
+
+       // mAddRAM.setSummary(formatFileSize(getTotalMemorySize(this),true));
+       // mAddFlash.setSummary(formatFileSize(getTotalInternalMemorySize(),true));
+        //
+   //此处想必各位一看就明白了  
+       setSummaryText(KEY_RAM, formatRamFileSize(getTotalMemorySize(this),false));
+       
+       
+       setSummaryText(KEY_FLASH, formatFileSize(getTotalInternalMemorySize()+getTotalMemorySize(this),false));
+
+      
+   //
+
         mRes = getResources();
         mUnknown = mRes.getString(R.string.device_info_default);
         mUnavailable = mRes.getString(R.string.status_unavailable);
@@ -235,10 +273,86 @@ public class Status extends InstrumentedPreferenceActivity {
             });
     }

-    @Override
-    protected int getMetricsCategory() {
-        return MetricsLogger.DEVICEINFO_STATUS;

+    public  long getTotalInternalMemorySize() {
+        File path = Environment.getDataDirectory();
+        StatFs stat = new StatFs(path.getPath());
+        long blockSize = stat.getBlockSize();
+        long totalBlocks = stat.getBlockCount();
+        return totalBlocks * blockSize;
     }
+    
+    public  long getTotalMemorySize(Context context) {
+        String dir = "/proc/meminfo";
+        try {
+            FileReader fr = new FileReader(dir);
+            BufferedReader br = new BufferedReader(fr, 2048);
+            String memoryLine = br.readLine();
+            String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
+            br.close();
+            return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024;
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return 0;
+    }
+
+    private  DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
+    private  DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
+   
+   
+    public String formatRamFileSize(long size, boolean isInteger) {
+        DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
+        String fileSizeString = "0M";
+        if (size < 1024 && size > 0) {
+            fileSizeString = df.format((double) size) + "B";
+        } else if (size < 1024 * 1024) {
+            fileSizeString = df.format((double) size / 1024) + "KB";
+        } else if (size < 1024 * 1024 * 1024) {
+            fileSizeString = df.format((double) size / (1024 * 1024)) + "MB";
+           if(((double) size / (1024 * 1024))<512){
+               fileSizeString="512MB";
+           }else if(((double) size / (1024 * 1024))<1024){
+               fileSizeString="1GB";
+           }
+        } else {
+            fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "GB";           
+        }
+        return fileSizeString;
+}
+
+
+    public String formatFileSize(long size, boolean isInteger) {
+        DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
+        String fileSizeString = "0M";
+        if (size < 1024 && size > 0) {
+            fileSizeString = df.format((double) size) + "B";
+        } else if (size < 1024 * 1024) {
+            fileSizeString = df.format((double) size / 1024) + "KB";
+        } else if (size < 1024 * 1024 * 1024) {
+            fileSizeString = df.format((double) size / (1024 * 1024)) + "MB";
+        } else {
+            fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "GB";
+           
+           if(((double) size / (1024 * 1024 * 1024))<4){
+               fileSizeString="4GB";
+           }else if(((double) size / (1024 * 1024 * 1024))<8){
+               fileSizeString="8GB";
+           }else if(((double) size / (1024 * 1024 * 1024))<16){
+               fileSizeString="16GB";
+           }else if(((double) size / (1024 * 1024 * 1024))<32){
+               fileSizeString="32GB";
+           }else if(((double) size / (1024 * 1024 * 1024))<64){
+               fileSizeString="64GB";
+           }
+        }
+        return fileSizeString;
+}

你可能感兴趣的:(Android客制化)