一、内存(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 = ()) != null) {
(TAG, "---" + str2);
}
} catch (IOException e) {
}
}
运行后的结果信息如下:
INFO/-SystemInfo-(1519): ---MemTotal: 204876 kB
INFO/-SystemInfo-(1519): ---MemFree: 4596 kB
INFO/-SystemInfo-(1519): ---Buffers: 16020 kB
INFO/-SystemInfo-(1519): ---Cached: 82508 kB
INFO/-SystemInfo-(1519): ---SwapCached: 64 kB
INFO/-SystemInfo-(1519): ---Active: 137104 kB
INFO/-SystemInfo-(1519): ---Inactive: 41056 kB
INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB
INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB
INFO/-SystemInfo-(1519): ---Dirty: 88 kB
INFO/-SystemInfo-(1519): ---Writeback: 0 kB
INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB
INFO/-SystemInfo-(1519): ---Mapped: 38296 kB
INFO/-SystemInfo-(1519): ---Slab: 5768 kB
INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB
INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB
INFO/-SystemInfo-(1519): ---PageTables: 8184 kB
INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB
INFO/-SystemInfo-(1519): ---Bounce: 0 kB
INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB
INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB
INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB
INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB
INFO/-SystemInfo-(1519): ---VmallocChunk: 674820 kB
第一行是总内存大小(即用户可以使用的ram的大小)!
获取当前剩余内存(ram)大小的方法:
public long getAvailMemory() {
ActivityManager am = (ActivityManager)(Context.ACTIVITY_SERVICE);
mi = new ();
(mi);
return ;
}
二、Rom大小
public long[] getRomMemroy() {
long[] romInfo = new long[2];
//Total rom memory
romInfo[0] = getTotalInternalMemorySize();
//Available rom memory
File path = ();
StatFs stat = new StatFs(());
long blockSize = ();
long availableBlocks = ();
romInfo[1] = blockSize * availableBlocks;
getVersion();
return romInfo;
}
public long getTotalInternalMemorySize() {
File path = ();
StatFs stat = new StatFs(());
long blockSize = ();
long totalBlocks = ();
return totalBlocks * blockSize;
}
注意类型,不然相乘之后会有溢出。可用内部存储的大小不能通过getRootDirectory();
取得,之前网上传的很多都是用getRootDirectory()取得的,我测试之后发现取得的数值不对。要根据getDataDirectory();
取得。
三、sdCard大小
public long[] getSDCardMemory() {
long[] sdCardInfo=new long[2];
String state = ();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = ();
StatFs sf = new StatFs(());
long bSize = ();
long bCount = ();
long availBlocks = ();
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 = ("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 = ();
arrayOfString = ("\\s+");
for (int i = 2; i < ; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = ();
arrayOfString = ("\\s+");
cpuInfo[1] += arrayOfString[2];
();
} 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 = ();
arrayOfString = ("\\s+");
version[0]=arrayOfString[2];//KernelVersion
();
} catch (IOException e) {
}
version[1] = ;// firmware version
version[2]=;//model
version[3]=;//system version
return version;
版本信息里面还包括型号等信息。
七、MAC地址和开机时间:
public String[] getOtherInfo(){
String[] other={"null","null"};
WifiManager wifiManager = (WifiManager) (Context.WIFI_SERVICE);
WifiInfo wifiInfo = ();
if(()!=null){
other[0]=();
} else {
other[0] = "Fail";
}
other[1] = getTimes();
return other;
}
private String getTimes() {
long ut = () / 1000;
if (ut == 0) {
ut = 1;
}
int m = (int) ((ut / 60) % 60);
int h = (int) ((ut / 3600));
return h + " " + (.info_times_hour) + m + " "
+ (.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;
}
df = new ("#0.00");
StringBuilder resultBuffer = new StringBuilder((fSize));
if (suffix != null)
(suffix);
return ();
}
保留两位小数。
八、获取其它手机相关信息
1、获取手机制造厂商
2、获取手机型号
3、获取手机系统当前使用的语言
4、获取Android系统版本号
5、获取手机IMEI串号
6、获取手机中的语言列表
SystemUtil类
/**
* Created by abc on 2019/7/2.
*/
public class SystemUtil extends Application{
public void showSystemParameter() {
String TAG = "系统参数:";
(TAG, "手机厂商:" + ());
(TAG, "手机型号:" + ());
(TAG, "手机当前系统语言:" + ());
(TAG, "Android系统版本号:" + ());
// (TAG, "手机IMEI:" + (getApplicationContext()));
}
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
public static String getSystemLanguage() {
return ().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*
* @return 语言列表
*/
public static Locale[] getSystemLanguageList() {
return ();
}
/**
* 获取当前手机系统版本号
*
* @return 系统版本号
*/
public static String getSystemVersion() {
return ;
}
/**
* 获取手机型号
*
* @return 手机型号
*/
public static String getSystemModel() {
return ;
}
/**
* 获取手机厂商
*
* @return 手机厂商
*/
public static String getDeviceBrand() {
return ;
}
/**
* 获取手机IMEI(需要“.READ_PHONE_STATE”权限)
*
* @return 手机IMEI
*/
@SuppressLint("MissingPermission")
public static String getIMEI(Context ctx) {
TelephonyManager tm= (TelephonyManager) (Activity.TELEPHONY_SERVICE);
try{
return ();
}catch (Exception e){
return null;
}
// if (tm != null) {
// return ();
// }
// return null;
}
}