Android之获得内存剩余大小与总大小

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_7202/article/details/82481941

转自: http://www.cnblogs.com/lee0oo0/archive/2012/11/23/2784642.html

 

public class memInfo {

 

    // 获得可用的内存

    public static long getmem_UNUSED(Context mContext) {

        long MEM_UNUSED;

// 得到ActivityManager

        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);// 创建ActivityManager.MemoryInfo对象          ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();

        am.getMemoryInfo(mi);

// 取得剩余的内存空间         MEM_UNUSED = mi.availMem / 1024;

        return MEM_UNUSED;

    }

 

    // 获得总内存

    public static long getmem_TOLAL() {

        long mTotal;

        // /proc/meminfo读出的内核信息进行解释

        String path = "/proc/meminfo";

        String content = null;

        BufferedReader br = null;

        try {

            br = new BufferedReader(new FileReader(path), 8);

            String line;

            if ((line = br.readLine()) != null) {

                content = line;

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (br != null) {

                try {

                    br.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

        // beginIndex

        int begin = content.indexOf(':');

        // endIndex

        int end = content.indexOf('k');

        // 截取字符串信息content = content.substring(begin + 1, end).trim();

        mTotal = Integer.parseInt(content);

        return mTotal;

    }

}

展开阅读全文

获得硬盘剩余容量大小问题!达人请帮忙!

08-20

小弟最近需要下一个计算机容量监视的东西:), rnrn我在网上找了获得硬盘剩余容量的代码,并且使用了,发现剩余容量大于2G以上的就无法正确读取, 请问高手能指点一下吗? rnrn下面是我的函数, 应该是 GetDiskFreeSpace 只能读取2G以内的吧,那请问读2G以上的需要怎么来写呢?请会的高手一定指导,谢谢了^^rnrnrn/*rn * 获得磁盘剩余空间大小rn */rnDWORD GetDiskSpace(char driver[])rnrn BOOL result = TRUE;rn DWORD SectorsPerCluster; // address of sectors per clusterrn DWORD BytesPerSector; // address of bytes per sectorrn DWORD NumberOfFreeClusters; // address of number of free clustersrn DWORD TotalNumberOfClusters;rn DWORD TotalBytes;rn DWORD FreeBytes;rn int bContinue = 1;rn char DiskVolumeSerialNumber[30]; // 存储驱动器内当前磁盘的序列号rn LPCTSTR lpRootPathName; // address of root directory of the file systemrn LPTSTR lpVolumeNameBuffer = new char[12]; // address of name of the volumern DWORD nVolumeNameSize = 12; // length of lpVolumeNameBufferrn DWORD VolumeSerialNumber; // address of volume serial numberrn DWORD MaximnmComponentLength; // address of system`s maximum filename lengthrn DWORD FileSystemFlags; // address of file system flagsrn LPTSTR lpFileSystemNameBuffer = new char[10]; // address of name of file systemrn DWORD nFileSystemNameSize = 10; // length of lpFileSystemNameBufferrn lpRootPathName = driver;rnrn while(1)rn rn if (GetDiskFreeSpace(driver, &SectorsPerCluster, &BytesPerSectorrn , &NumberOfFreeClusters, &TotalNumberOfClusters))rn // 驱动器中有磁盘rn TotalBytes = SectorsPerCluster*BytesPerSector *TotalNumberOfClusters; // 磁盘总容量rn FreeBytes = SectorsPerCluster* BytesPerSector *NumberOfFreeClusters; // 磁盘空闲容量rn GetVolumeInformation(lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize, &VolumeSerialNumber, &MaximnmComponentLength, &FileSystemFlags, lpFileSystemNameBuffer, nFileSystemNameSize);rnrn sprintf(DiskVolumeSerialNumber, "%X", VolumeSerialNumber); // 得到驱动器内当前磁盘的序列号rn rn // SetTotalBytes(TotalBytes / 1024); // 存储指定驱动器中磁盘的容量rn return FreeBytes;rn rn elsern rn return 0;rn rn rnrn 论坛

没有更多推荐了,返回首页