android 手机内存SD卡和cpu等信息的获取

来源:互联网 发布:java 表格 编辑:程序博客网 时间:2024/05/17 00:56

一、内存(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.    } 


二、获取SD卡(外部存储)和机身内存(内部存储也叫rom)


01./**
02.     * 获得SD卡总大小
03.     * 
04.     * @return
05.     */ 
06.    private String getSDTotalSize() { 
07.        File path = Environment.getExternalStorageDirectory(); 
08.        StatFs stat = new StatFs(path.getPath()); 
09.        long blockSize = stat.getBlockSize(); 
10.        long totalBlocks = stat.getBlockCount(); 
11.        return Formatter.formatFileSize(MainActivity.this, blockSize * totalBlocks); 
12.    } 
13. 
14.    /**
15.     * 获得sd卡剩余容量,即可用大小
16.     * 
17.     * @return
18.     */ 
19.    private String getSDAvailableSize() { 
20.        File path = Environment.getExternalStorageDirectory(); 
21.        StatFs stat = new StatFs(path.getPath()); 
22.        long blockSize = stat.getBlockSize(); 
23.        long availableBlocks = stat.getAvailableBlocks(); 
24.        return Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); 
25.    } 
26. 
27.    /**
28.     * 获得机身内存总大小(手机总共的内存空间)
29.     * 
30.     * @return
31.     */ 
32.    private String getRomTotalSize() { 
33.        File path = Environment.getDataDirectory(); 
34.        StatFs stat = new StatFs(path.getPath()); 
35.        long blockSize = stat.getBlockSize(); 
36.        long totalBlocks = stat.getBlockCount(); 
37.        return Formatter.formatFileSize(MainActivity.this, blockSize * totalBlocks); 
38.    } 
39. 
40.    /**
41.     * 获得机身可用内存(手机剩余可用的内存空间)
42.     * 
43.     * @return
44.     */ 
45.    private String getRomAvailableSize() { 
46.        File path = Environment.getDataDirectory(); 
47.        StatFs stat = new StatFs(path.getPath()); 
48.        long blockSize = stat.getBlockSize(); 
49.        long availableBlocks = stat.getAvailableBlocks(); 
50.        return Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); 
51.    } 

 

 

 

三、电池电量

 

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.} 


垃圾清理 :

a类.应用缓存

    应用程序data/data下缓存

b类.残留清理

 系统临时文件
 手机系统缩略图缓存
 日志文件
 空文件夹
 广告文件
 程序卸载残留

 


android 清理卸载应用残留文件原理

用户在刚开始运行程序的时候,先检查是否联网,如果联网,则将本地当 前文件特征库上传到服务器
服务器将服务器的文件特征库新版本号与客户端本地版本号 作比较,查看是否需要更新。如果需要更新,
返回需要更新标识,客户端弹框提示用户需要 更新文件特征库。当用户完成更新操作后,用户选择扫描目录,
客户端通过扫描引擎扫描指 定目录,获取该目录下的文件信息。将该文件信息与文件特征库进行
匹配(文件特征库主要90 由应用程序,应用程序生成的文件和文件夹路径组成),通过对比获得该文件信息对应的应用程序,
然后查看该应用程序是否被卸载,如果被卸载,则该文件是残留文件,建议用户清 除该文件。

 


 

0 0
原创粉丝点击