Android 系统信息的获取

一、内存(ram):

1、android 的内存大小信息存放在系统的 /proc/meminfo 文件里面,通过adb shell 进入 手机目录后,使用 cat /proc/meminfo 可以显示这些信息:

root@m7:/ # cat /proc/meminfo
MemTotal: 1863064 kB
MemFree: 109396 kB
Buffers: 30892 kB
Cached: 835468 kB
SwapCached: 0 kB
Active: 855868 kB
Inactive: 593304 kB
Active(anon): 424552 kB
Inactive(anon): 162992 kB
Active(file): 431316 kB
Inactive(file): 430312 kB
Unevictable: 2268 kB
Mlocked: 0 kB
HighTotal: 1458172 kB
HighFree: 35356 kB
LowTotal: 404892 kB
LowFree: 74040 kB
SwapTotal: 419428 kB
SwapFree: 412148 kB
Dirty: 60 kB
Writeback: 0 kB
AnonPages: 585116 kB
Mapped: 272388 kB
Shmem: 2460 kB
Slab: 52480 kB
SReclaimable: 23556 kB
SUnreclaim: 28924 kB
KernelStack: 14736 kB
PageTables: 35532 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1350960 kB
Committed_AS: 82837660 kB
VmallocTotal: 409600 kB
VmallocUsed: 181572 kB
VmallocIoRemap: 172600 kB
VmallocAlloc: 8376 kB
VmallocMap: 0 kB
VmallocUserMap: 0 kB
VmallocVpage: 4888 kB
VmallocChunk: 69636 kB
KGSL_ALLOC: 34340 kB
ION_ALLOC: 105376 kB
Kmalloc: 8112 kB
DMA ALLOC: 352 kB
CachedUnmapped: 578420 kB
KgslUnmapped: 19528 kB
Ftrace: 32768 kB

2、在代码中可以通过读取 /proc/meminfo 文件来获取相应的信息:

    public void getTotalRamMemory() {
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) {
}
}

3、也可以通过 ActivityManager.MemoryInfo 来获取相应的信息:

    public long getAvailRamMemory(Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}

二、机身容量(Rom大小)

1、android 机身容量,通过adb shell 进入 手机目录后,使用 df 命令可以显示手机挂载分区详细信息:

root@m7:/ # df
Filesystem Size Used Free Blksize
/dev 909.7M 68.0K 909.6M 4096
/sys/fs/cgroup 909.7M 12.0K 909.7M 4096
/mnt/asec 909.7M 0.0K 909.7M 4096
/mnt/obb 909.7M 0.0K 909.7M 4096
/system 1763.8M 1589.9M 174.0M 4096
/data 26206.2M 3082.8M 23123.4M 4096
/cache 629.9M 10.5M 619.4M 4096
/devlog 19.7M 15.8M 3.9M 4096
/carrier 24.6M 4.0M 20.6M 4096
/firmware/mdm 86.0M 69.1M 16.8M 16384
/data/qcks 20.0M 9.0M 11.0M 4096
/data/efs 20.0M 0.0K 20.0M 4096
/firmware/q6 199.8M 6.4M 193.3M 4096
/data/secure/data 909.7M 0.0K 909.7M 4096
/mnt/shell/emulated 26106.2M 3082.8M 23023.4M 4096
/sqlite_stmt_journals 512.0K 0.0K 512.0K 4096

2、在代码中可以通过 df  命令来显示机身容量,这个方法也适用与获取sdCard的大小:

    /**
* get "/data" block available size.
*
* @return available size(kB).
*/
public static long getDataAvailable() {
// exec cmd "df" & get column as: Filesystem Size Used Free Blksize;
// the 3th column of /data is dataFreeSize.
String dataFreeSizeDetailInfo = getDeviceSystemInfo("df", "/data ", 3)
.trim();
// remove the last char of the dataFreeSizeDetailInfo
String dataFreeSizeShotInfo = dataFreeSizeDetailInfo.substring(0,
dataFreeSizeDetailInfo.length() - 1);
float dataFreeSize = Float.parseFloat(dataFreeSizeShotInfo);
float dataFreeKB = dataFreeSize;
if (dataFreeSizeDetailInfo.endsWith("G")// GB to kB
|| dataFreeSizeDetailInfo.endsWith("g")) {
dataFreeKB = dataFreeSize * (1024 * 1024);
} else if (dataFreeSizeDetailInfo.endsWith("M")// MB to kB
|| dataFreeSizeDetailInfo.endsWith("m")) {
dataFreeKB = dataFreeSize * 1024;
} else {// kB to kB
}
return (long) dataFreeKB;
} /**
* get device system info by executing shell command.
*
* @param cmd
* command to execute by using device shell.
* @param target
* target line or some special line that contain the target
* string ,such as "/data"
* @param column
* ={0,1,2,3,...}
* @return
*/
public static String getDeviceSystemInfo(String cmd, String target,
int column) {
String result = "";
result = RootCmd.execShellCmd(cmd);
String[] strs = result.split("\n");
String info = "-1";
for (String str : strs) {
if (str.startsWith(target) && str.contains(target)) { Pattern p = Pattern.compile("\\s+");
Matcher m = p.matcher(str);
str = m.replaceAll(" "); String[] subStr = str.split(" ");
if (subStr.length < column) {
return null;
}
String columnInfo = subStr[column];
info = columnInfo;
}
}
return info;
}

3、也可以通过 StatFs 来获取机身容量:

    /**
*
* 获得机身内存大小
*
* @return
*/
public long getRomTotalSize() {
File path = Environment.getDataDirectory();
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSizeLong();
long tatalBlocks = statFs.getBlockCountLong();
return blockSize * tatalBlocks;
} /**
* 获得机身可用内存
*
* @return
*/
private long getRomAvailableSize() {
File path = Environment.getDataDirectory();
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSizeLong();
long availableBlocks = statFs.getAvailableBlocksLong();
return blockSize * availableBlocks;
}

三、sdCard大小

    /**
*
* 获得sd卡总共容量
*
* @return
*/
public long getSDTotalSize() {
/* 获取存储卡路径 */
File sdcardDir = Environment.getExternalStorageDirectory();
/* StatFs 看文件体系空间应用景象 */
StatFs statFs = new StatFs(sdcardDir.getPath());
long blockSize = statFs.getBlockSizeLong();
long totalSize = statFs.getBlockCountLong();
return blockSize * totalSize;
} /**
*
* 获得sd卡残剩容量,即可以大小
*
* @return
*/
public long getSdAvaliableSize() {
File path = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSizeLong();
long availableBlocks = statFs.getAvailableBlocksLong();
return blockSize * availableBlocks;
}

四、取得的rom容量和sdcard容量时,常常需要将数据格式化,转换成KB,MB,GB,下面是一个格式化数据的方法: 保留两位小数。

    /**
* convert size to KB or MB or GB
*
* @param size
* @return
*/
protected String formatSize(long originSize) {
String suffix = null;
float fSize = 0; if (originSize >= 1024) {
suffix = "KB";
fSize = originSize / 1024;
if (fSize >= 1024) {
suffix = "MB";
fSize /= 1024;
}
if (fSize >= 1024) {
suffix = "GB";
fSize /= 1024;
}
} else {
fSize = originSize;
}
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();
}

五、CPU信息

1、通过adb shell 进入 手机目录后,使用 cat /proc/cpuinfo 可以显示 CPU 详细信息,/proc/cpuinfo文件中第一行是CPU的型号,第二行是CPU的频率

root@m7:/ # cat /proc/cpuinfo
Processor : ARMv7 Processor rev 0 (v7l)
processor : 0
BogoMIPS : 13.52 processor : 1
BogoMIPS : 13.52 processor : 2
BogoMIPS : 13.52 processor : 3
BogoMIPS : 13.52 Features : swp half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpv4
CPU implementer : 0x51
CPU architecture: 7
CPU variant : 0x1
CPU part : 0x06f
CPU revision : 0 Hardware : UNKNOWN
Revision : 0002
Serial : 0000000000000000

2、在代码中可以通过读取 /proc/cpuinfo 文件来获取CPU相应的信息:

    /**
* get CPU info
*
* @return
*/
private String getCPUInfo() {
StringBuffer sb = new StringBuffer();
sb.append("abi: ").append(Build.CPU_ABI).append("\n");
if (new File("/proc/cpuinfo").exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
"/proc/cpuinfo")));
String aLine;
while ((aLine = br.readLine()) != null) {
sb.append(aLine + "\n");
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

六、系统的版本信息:

    /**
* get system version
*
* @return
*/
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地址:

    /**
*
* @return
*/
public String getMacInfo(Context context) {
String macInfo;
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getMacAddress() != null) {
macInfo = wifiInfo.getMacAddress();
} else {
macInfo = "Fail";
}
return macInfo;
}

八、电池电量:

    /**
* register battery broadcast receiver
*
* @param context
*/
public void registerBatteryReceiver(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
context.registerReceiver(batteryReceiver, filter, null, null);
} /**
* unregister battery broadcast receiver
*
* @param context
*/
public void unregisterBatteryReceiver(Context context) {
context.unregisterReceiver(batteryReceiver);
} private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
// level加%就是当前电量了
System.out.print("Current battery level:" + level);
}
};

九、开机时间:

    /**
*
* @return
*/
private String getStartTimes(Context context) {
long ut = SystemClock.elapsedRealtime() / 1000;
if (ut == 0) {
ut = 1;
}
int m = (int) ((ut / 60) % 60);
int h = (int) ((ut / 3600));
return h + " hours," + m + " minutes.";
}

Android 系统信息的获取的更多相关文章

  1. Android群英传笔记——第九章:Android系统信息和安全机制

    Android群英传笔记--第九章:Android系统信息和安全机制 本书也正式的进入尾声了,在android的世界了,不同的软件,硬件信息就像一个国家的经济水平,军事水平,不同的配置参数,代表着一个 ...

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

    Android系统信息(内存.cpu.sd卡.电量.版本)获取 /*APPInfo.java*/ public class AppInfo { private String appLable; pri ...

  3. xamarin.android之 Android 4.4+ 获取图片真实路径

    Android 4.4以下 选择图片是可以获取到图片路径的.高于Android 4.4获取图片路径只是获取到一个图片编号. 所以需要针对Android版本进行路径解析: #region 高于 v4.4 ...

  4. android 通过socket获取IP

    如题<android 通过socket获取IP>: socket.getInetAddress().getHostAddress();

  5. 【风马一族_Android】Android 从命令行界面获取手机信息

    Android 从命令行界面获取手机信息 1: cmd 打开命令行界面 2:adb devices   获取与电脑相连的设备,例如:模拟器.真机(手机) (右击“标记”,选择设备名称,点击“Ctrl+ ...

  6. Android开发之获取xml文件的输入流对象

    介绍两种Android开发中获取xml文件的输入流对象 第一种:通过assets目录获取 1.首先是在Project下app/src/main目录下创建一个assets文件夹,将需要获取的xml文件放 ...

  7. vc++基础班[25]---系统信息的获取

    --------------------------------------------------------------------------- VC 驿站 WwW.CcTry.CoM 多抽出一 ...

  8. Android SurfaceView内容获取

    Android SurfaceView内容获取 这几天在做一个Android的小项目,项目中需要使用到SurfaceView来显示相机捕获的内容,同时还有一个SurfaceView用于绘制一些辅助线, ...

  9. 【Android】如何获取本机号码、IMSI、EMSI

    获取本机号码: 获取本机号码,需要在配置文件中加入权限: <uses-permission android:name="android.permission.READ_PHONE_ST ...

随机推荐

  1. 玩玩nmap

    ---恢复内容开始--- [root@miyan ~]# nmap -v Starting Nmap 7.12 ( https://nmap.org ) at 2016-04-04 15:34 CST ...

  2. ruby项目文件上传功能实现

    这里我将从视图.控制器各个层面进行讲解. rails 提供了文件上传功能,可以直接进行下面的编码 <%= form_for :document, :html =>{:multipart = ...

  3. 动态切换数据库(EF框架)

             文章简略:本文测试项目为Silverlight+EF+RIA Service动态切换数据库的问题 通常,Ado.net EntityFramework的数据库连接字符串Connect ...

  4. mongodb-2.6.0 在win7 64下的安装和服务启动

    转自: http://blog.csdn.net/lingchen214/article/details/24537629 1   自定义安装到C:\mongodb目录下. 2  手动在C:\mong ...

  5. springmvc get post put delete

    web.xml <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POS ...

  6. PAT 天梯赛 L1-005. 考试座位号 【MAP标记】

    题目链接 https://www.patest.cn/contests/gplt/L1-005 题意 有一个 考生号,一个试机座位,一个考试座位,给出试机座位,查询 考生号和考试座位 思路 MAP + ...

  7. hadoop23---自定义rpc架构(duboo的原理)

  8. Maven:如何在eclipse里新建一个Maven的java项目和web项目

    如何在eclipse里新建一个Maven的java项目和web项目: 一:java项目 New-->Other-->Maven 右击项目-->properties,修改以下文件: ① ...

  9. [翻译]在gulp构建工具中使用PostCSS

    前言 PostCSS已经在一段时间内迅速普及,如果你还不知道PostCSS或还没有使用它,我建议你看一下之前的一篇介绍文章<PostCSS简介>,其中介绍了使用PostCSS的基本方法,包 ...

  10. 什么是Zero-Copy?

    概述 考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那么这个情形就意味着你需要先将静态内容从磁盘中拷贝出来放到一个内存buf中,然后将这个buf通过socket传输给用户,进而 ...