一、内存(ram):
android的总内存大小信息存放在系统的/proc/meminfo文件里面,可以通过读取这个文件来获取这些信息:
- public void getTotalMemory() {
- String str1 = "/proc/meminfo";
- String str2="";
- try {
- FileReader fr = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
- while ((str2 = localBufferedReader.readLine()) != null) {
- Log.i(TAG, "---" + str2);
- }
- } catch (IOException e) {
- }
- }
- public void getTotalMemory() {
- String str1 = "/proc/meminfo";
- String str2="";
- try {
- FileReader fr = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
- while ((str2 = localBufferedReader.readLine()) != null) {
- Log.i(TAG, "---" + str2);
- }
- } catch (IOException e) {
- }
- }
运行信息如下:
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemTotal: 204876 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree: 4596 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers: 16020 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached: 82508 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached: 64 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active: 137104 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive: 41056 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty: 88 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback: 0 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped: 38296 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab: 5768 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables: 8184 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce: 0 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocChunk: 674820 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemTotal: 204876 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree: 4596 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers: 16020 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached: 82508 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached: 64 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active: 137104 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive: 41056 kB
- 05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty: 88 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback: 0 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped: 38296 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab: 5768 kB
- 05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables: 8184 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce: 0 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB
- 05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB
- 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)大小的方法:
- public long getAvailMemory() {
- ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
- ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
- am.getMemoryInfo(mi);
- return mi.availMem;
- }
- public long getAvailMemory() {
- ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
- ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
- am.getMemoryInfo(mi);
- return mi.availMem;
- }
二、Rom大小
- public long[] getRomMemroy() {
- long[] romInfo = new long[2];
- //Total rom memory
- romInfo[0] = getTotalInternalMemorySize();
- //Available rom memory
- File path = Environment.getDataDirectory();
- StatFs stat = new StatFs(path.getPath());
- long blockSize = stat.getBlockSize();
- long availableBlocks = stat.getAvailableBlocks();
- romInfo[1] = blockSize * availableBlocks;
- getVersion();
- return romInfo;
- }
- 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[] getRomMemroy() {
- long[] romInfo = new long[2];
- //Total rom memory
- romInfo[0] = getTotalInternalMemorySize();
- //Available rom memory
- File path = Environment.getDataDirectory();
- StatFs stat = new StatFs(path.getPath());
- long blockSize = stat.getBlockSize();
- long availableBlocks = stat.getAvailableBlocks();
- romInfo[1] = blockSize * availableBlocks;
- getVersion();
- return romInfo;
- }
- public long getTotalInternalMemorySize() {
- File path = Environment.getDataDirectory();
- StatFs stat = new StatFs(path.getPath());
- long blockSize = stat.getBlockSize();
- long totalBlocks = stat.getBlockCount();
- return totalBlocks * blockSize;
- }
注意类型,不然相乘之后会有溢出。可用内部存储的大小不能通过getRootDirectory();
取得,之前网上传的很多都是用getRootDirectory()取得的,我测试之后发现取得的数值不对。要根据getDataDirectory();
取得。
三、sdCard大小
- public long[] getSDCardMemory() {
- long[] sdCardInfo=new long[2];
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- File sdcardDir = Environment.getExternalStorageDirectory();
- StatFs sf = new StatFs(sdcardDir.getPath());
- long bSize = sf.getBlockSize();
- long bCount = sf.getBlockCount();
- long availBlocks = sf.getAvailableBlocks();
- sdCardInfo[0] = bSize * bCount;//总大小
- sdCardInfo[1] = bSize * availBlocks;//可用大小
- }
- return sdCardInfo;
- }
- public long[] getSDCardMemory() {
- long[] sdCardInfo=new long[2];
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- File sdcardDir = Environment.getExternalStorageDirectory();
- StatFs sf = new StatFs(sdcardDir.getPath());
- long bSize = sf.getBlockSize();
- long bCount = sf.getBlockCount();
- long availBlocks = sf.getAvailableBlocks();
- sdCardInfo[0] = bSize * bCount;//总大小
- sdCardInfo[1] = bSize * availBlocks;//可用大小
- }
- return sdCardInfo;
- }
注意类型,不然相乘之后会有溢出。
四、电池电量
- private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
- @Override
- public void onReceive(Context context, Intent intent) {
- int level = intent.getIntExtra("level", 0);
- // level加%就是当前电量了
- }
- };
- private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
- @Override
- public void onReceive(Context context, Intent intent) {
- int level = intent.getIntExtra("level", 0);
- // level加%就是当前电量了
- }
- };
然后在activity的oncreate()方法中注册
- registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
- registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
五、CPU信息
- public String[] getCpuInfo() {
- String str1 = "/proc/cpuinfo";
- String str2="";
- String[] cpuInfo={"",""};
- String[] arrayOfString;
- try {
- FileReader fr = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- for (int i = 2; i < arrayOfString.length; i++) {
- cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
- }
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- cpuInfo[1] += arrayOfString[2];
- localBufferedReader.close();
- } catch (IOException e) {
- }
- return cpuInfo;
- }
- public String[] getCpuInfo() {
- String str1 = "/proc/cpuinfo";
- String str2="";
- String[] cpuInfo={"",""};
- String[] arrayOfString;
- try {
- FileReader fr = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- for (int i = 2; i < arrayOfString.length; i++) {
- cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
- }
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- cpuInfo[1] += arrayOfString[2];
- localBufferedReader.close();
- } catch (IOException e) {
- }
- return cpuInfo;
- }
/proc/cpuinfo文件中第一行是CPU的型号,第二行是CPU的频率,可以通过读文件,读取这些数据!
六、系统的版本信息:
- public String[] getVersion(){
- String[] version={"null","null","null","null"};
- String str1 = "/proc/version";
- String str2;
- String[] arrayOfString;
- try {
- FileReader localFileReader = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(
- localFileReader, 8192);
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- version[0]=arrayOfString[2];//KernelVersion
- localBufferedReader.close();
- } catch (IOException e) {
- }
- version[1] = Build.VERSION.RELEASE;// firmware version
- version[2]=Build.MODEL;//model
- version[3]=Build.DISPLAY;//system version
- return version;
- }
- public String[] getVersion(){
- String[] version={"null","null","null","null"};
- String str1 = "/proc/version";
- String str2;
- String[] arrayOfString;
- try {
- FileReader localFileReader = new FileReader(str1);
- BufferedReader localBufferedReader = new BufferedReader(
- localFileReader, 8192);
- str2 = localBufferedReader.readLine();
- arrayOfString = str2.split("\\s+");
- version[0]=arrayOfString[2];//KernelVersion
- localBufferedReader.close();
- } catch (IOException e) {
- }
- version[1] = Build.VERSION.RELEASE;// firmware version
- version[2]=Build.MODEL;//model
- version[3]=Build.DISPLAY;//system version
- return version;
- }
版本信息里面还包括型号等信息。
七、MAC地址和开机时间:
- public String[] getOtherInfo(){
- String[] other={"null","null"};
- WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- if(wifiInfo.getMacAddress()!=null){
- other[0]=wifiInfo.getMacAddress();
- } else {
- other[0] = "Fail";
- }
- other[1] = getTimes();
- return other;
- }
- private String getTimes() {
- long ut = SystemClock.elapsedRealtime() / 1000;
- if (ut == 0) {
- ut = 1;
- }
- int m = (int) ((ut / 60) % 60);
- int h = (int) ((ut / 3600));
- return h + " " + mContext.getString(R.string.info_times_hour) + m + " "
- + mContext.getString(R.string.info_times_minute);
- }
- public String[] getOtherInfo(){
- String[] other={"null","null"};
- WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- if(wifiInfo.getMacAddress()!=null){
- other[0]=wifiInfo.getMacAddress();
- } else {
- other[0] = "Fail";
- }
- other[1] = getTimes();
- return other;
- }
- private String getTimes() {
- long ut = SystemClock.elapsedRealtime() / 1000;
- if (ut == 0) {
- ut = 1;
- }
- int m = (int) ((ut / 60) % 60);
- int h = (int) ((ut / 3600));
- return h + " " + mContext.getString(R.string.info_times_hour) + m + " "
- + mContext.getString(R.string.info_times_minute);
- }
最后贴一个格式化数据的方法:
- public String formatSize(long size) {
- String suffix = null;
- float fSize=0;
- if (size >= 1024) {
- suffix = "KB";
- fSize=size / 1024;
- if (fSize >= 1024) {
- suffix = "MB";
- fSize /= 1024;
- }
- if (fSize >= 1024) {
- suffix = "GB";
- fSize /= 1024;
- }
- } else {
- fSize = size;
- }
- java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
- StringBuilder resultBuffer = new StringBuilder(df.format(fSize));
- if (suffix != null)
- resultBuffer.append(suffix);
- return resultBuffer.toString();
- }
- public String formatSize(long size) {
- String suffix = null;
- float fSize=0;
- if (size >= 1024) {
- suffix = "KB";
- fSize=size / 1024;
- if (fSize >= 1024) {
- suffix = "MB";
- fSize /= 1024;
- }
- if (fSize >= 1024) {
- suffix = "GB";
- fSize /= 1024;
- }
- } else {
- fSize = size;
- }
- java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
- StringBuilder resultBuffer = new StringBuilder(df.format(fSize));
- if (suffix != null)
- resultBuffer.append(suffix);
- return resultBuffer.toString();
- }
保留两位小数。
---------------------------------------------------------------------------------------
Android获取手机总内存和可用内存
在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小。这个该如何实现呢?通过读取文件”/proc/meminfo”的信息能够获取手机Memory的总量,而通过ActivityManager.getMemoryInfo(ActivityManager.MemoryInfo)方法可以获取当前的可用Memory量。
“/proc/meminfo”文件记录了android手机的一些内存信息,在命令行窗口里输入”adb shell”,进入shell环境,输入”cat /proc/meminfo”即可在命令行里显示meminfo文件的内容,具体如下所示。
C:\Users\Figo>adb shell # cat /proc/meminfo cat /proc/meminfo MemTotal: 94096 kB MemFree: 1684 kB Buffers: 16 kB Cached: 27160 kB SwapCached: 0 kB Active: 35392 kB Inactive: 44180 kB Active(anon): 26540 kB Inactive(anon): 28244 kB Active(file): 8852 kB Inactive(file): 15936 kB Unevictable: 280 kB Mlocked: 0 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 52688 kB Mapped: 17960 kB Slab: 3816 kB SReclaimable: 936 kB SUnreclaim: 2880 kB PageTables: 5260 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 47048 kB Committed_AS: 1483784 kB VmallocTotal: 876544 kB VmallocUsed: 15456 kB VmallocChunk: 829444 kB #
下面先对”/proc/meminfo”文件里列出的字段进行粗略解释:
MemTotal: 所有可用RAM大小。
MemFree: LowFree与HighFree的总和,被系统留着未使用的内存。
Buffers: 用来给文件做缓冲大小。
Cached: 被高速缓冲存储器(cache memory)用的内存的大小(等于diskcache minus SwapCache)。
SwapCached:被高速缓冲存储器(cache memory)用的交换空间的大小。已经被交换出来的内存,仍然被存放在swapfile中,用来在需要的时候很快的被替换而不需要再次打开I/O端口。
Active: 在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,除非非常必要,否则不会被移作他用。
Inactive: 在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径。
SwapTotal: 交换空间的总大小。
SwapFree: 未被使用交换空间的大小。
Dirty: 等待被写回到磁盘的内存大小。
Writeback: 正在被写回到磁盘的内存大小。
AnonPages:未映射页的内存大小。
Mapped: 设备和文件等映射的大小。
Slab: 内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗。
SReclaimable:可收回Slab的大小。
SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)。
PageTables:管理内存分页页面的索引表的大小。
NFS_Unstable:不稳定页表的大小。
要获取android手机总内存大小,只需读取”/proc/meminfo”文件的第1行,并进行简单的字符串处理即可。
JAVA代码如下:
package com.figo.readsyememory;
import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.TextView;
public class ReadSystemMemory extends Activity {
TextView tv = null;
private String getAvailMemory() {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);//mi.availMem; 当前系统的可用内存
return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
}
private String getTotalMemory() {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2; String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
arrayOfString = str2.split("\\s+");
for (String num : arrayOfString) {
Log.i(str2, num + "\t");
}
initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();
} catch (IOException e) { }
return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化 }
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.system_memory);
tv.setText("手机总内存: " + this.getTotalMemory() + ", " + "可用内存: "+ this.getAvailMemory());
}
}
运行结果如下:
令一种获取手机内存的方法:
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
blockSize = stat.getBlockSize();
AllSize = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
long AvailableSize=((availableBlocks*blockSize)/1024)/1024; //便得到了可用内存大小
long TotalSize=((AllSize*blockSize)/1024)/1024; //便得到了总内存大小
大功告成,顺利读取android手机的总内存和当前的可用内存。这里只是抛砖引玉,大家可以举一反三,进行扩展。当然我们还可以通过读取”/proc/cupinfo”来获取android手机的CPU参数,通过读取”/proc/stat”文件来计算CPU的使用率,这里不再赘述。
http://gqdy365.iteye.com/blog/1066113