Android -- 系统信息(内存、cpu、sd卡、电量、版本)获取

内存(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) {
        }
    }

运行信息如下:

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的大小)!

获取当前剩余内存(ram)大小的方法:

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

注意类型,不然相乘之后会有溢出。可用内部存储的大小不能通过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;
    }

注意类型,不然相乘之后会有溢出。

电池电量                                                                                   

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));

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

/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;
}

版本信息里面还包括型号等信息。

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);
}

我是天王盖地虎的分割线                                                               

参考:http://gqdy365.iteye.com/blog/1066113

Android -- 系统信息(内存、cpu、sd卡、电量、版本)获取

时间: 2024-10-06 14:16:26

Android -- 系统信息(内存、cpu、sd卡、电量、版本)获取的相关文章

Android设备内存和SD卡操作工具类

package cc.c; import java.io.File; import java.util.List; import android.os.StatFs; import java.io.FileReader; import java.io.IOException; import java.io.BufferedReader; import android.os.Environment; import android.content.Context; import android.ap

Android手机内存,SD卡读写

在手机上有两个存储位置 1 手机内部存储 2 SD卡 文件操作模式: 是否允许外部访问? 文件以覆盖/追加方式写? 手机内存读写 //保存文件到手机内存 public void save(String fileName,String content) throws IOException{ FileOutputStream fos = context.openFileOutput("a.txt", Context.MODE_PRIVATE); fos.write(content.get

Android学习笔记-获取手机内存,SD卡存储空间。

前面介绍到如何保存数据到手机内存或者SD卡,但是问题是,在保存以前,我们还需要对他们的空间(可用空间),进行判断,才可以进行后续操作,所以,本节我们就介绍如何获取手机内存以及Sd卡的空间. //这时获取手机内存的 // File path = Environment.getDataDirectory(); //这时获取SD卡的空间 File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(pat

android文件保存到sd卡和内存

1.保存到SD卡中: 获取SDCard的状态: Environment.getExtemalStorageState() Environment.MEDIA_MOUNTED手机装有SDCard,并且可以进行读写 获取SDCard的目录:Environment.getExtemalStorageDirectory() //============================================= File saveFile=new File("/sdcard/zhzhg.txt&q

获取网络图片(Bitmap)至内存或者SD卡

/** * 获取网络图片 * 注意权限: * <uses-permission android:name="android.permission.INTERNET"/> */ private Bitmap getBitmapFromNetWork(String imageUrl){ URL url=null; Bitmap bitmap=null; InputStream inputStream=null; HttpURLConnection httpURLConnecti

Android USB大容量存储时SD卡状态监听(转)

对SD卡状态监听,到现在为止我知道的有两种方式: 1.注册StorageEventListener来监听sd卡状态 StorageEventListener中有onStorageStateChanged()方法,当sd卡状态改变时,此方法会调用,对各状态的判断一般会用到Environment类,此类中包含的有关sd卡状态的常量有: MEDIA_BAD_REMOVAL:表明SDCard 被卸载前己被移除 MEDIA_CHECKING:表明对象正在磁盘检查 MEDIA_MOUNTED:表明sd对象是

[android] 保存文件到SD卡

手机里面有两块空间,手机内部空间(/data/data/)和外部存储空间(/mnt/sdcard/ 或者直接/sdcard/) 与上面的代码基本一样,只是在new File(“/sdcard/文件名”),此时会报错,primission denied Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) 在清单文件中添加权限 android.primission.WRITE_EXTERNAL_S

Android -- 写xml到SD卡中

信息类                                                                                          private String body; private String number; private int type; private long id; 四个变量,然后有分别set和get.构造函数分别初始化这四个变量. 生成xml标签函数                                   

android点滴之标准SD卡状态变化事件广播接收者的注册

目前最完整的,需要注册的动作匹配如下: IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); intentFilter.addAction(Intent.ACTION_ME

Android数据存储之SD卡

为了更好的存取应用程序的大文件数据,应用程序需要读. 写SD卡上的文件.SD卡大大扩充手机的存储能力. 操作SD首先要加权限: <!--在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name=&q