-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSystemUtils.java
549 lines (503 loc) · 16.4 KB
/
SystemUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.example.user.utils.sys;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Debug;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import com.example.user.utils.file.FileUtil;
import com.example.user.utils.string.StringUtil;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
/**
* 获取手机系统相关的工具类
* Created by zhangshixin on 2015/11/26.
* Blog : http://blog.csdn.net/u011240877
*
* @description Codes there always can be better.
*/
public class SystemUtils {
private static String TAG = SystemUtils.class.getSimpleName();
/** 获取android系统版本号 */
public static String getOSVersion() {
String release = android.os.Build.VERSION.RELEASE; // android系统版本号
release = "android" + release;
return release;
}
/** 获得android系统sdk版本号 */
public static String getOSVersionSDK() {
return android.os.Build.VERSION.SDK;
}
/** 获得android系统sdk版本号 */
public static int getOSVersionSDKINT() {
return android.os.Build.VERSION.SDK_INT;
}
/** 获取手机型号 */
public static String getDeviceModel() {
return android.os.Build.MODEL;
}
/** 获取设备的IMEI */
public static String getIMEI(Context context) {
if (null == context) {
return null;
}
String imei = null;
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
imei = tm.getDeviceId();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return imei;
}
/** 检测手机是否已插入SIM卡 */
public static boolean isCheckSimCardAvailable(Context context) {
if (null == context) {
return false;
}
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSimState() == TelephonyManager.SIM_STATE_READY;
}
/** sim卡是否可读 */
public static boolean isCanUseSim(Context context) {
if (null == context) {
return false;
}
try {
TelephonyManager mgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return TelephonyManager.SIM_STATE_READY == mgr.getSimState();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return false;
}
/** 取得当前sim手机卡的imsi */
public static String getIMSI(Context context) {
if (null == context) {
return null;
}
String imsi = null;
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
imsi = tm.getSubscriberId();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return imsi;
}
/** 返回本地手机号码,这个号码不一定能获取到 */
public static String getNativePhoneNumber(Context context) {
if (null == context) {
return null;
}
TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String NativePhoneNumber = null;
NativePhoneNumber = telephonyManager.getLine1Number();
return NativePhoneNumber;
}
/** 返回手机服务商名字 */
public static String getProvidersName(Context context) {
String ProvidersName = null;
String IMSI = getIMSI(context);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
} else {
ProvidersName = "其他服务商:" + IMSI;
}
return ProvidersName;
}
/**
* 判断当前网络状态
*
* @param mcontext
* @return
*/
public static String getCurrentNetState(Context mcontext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mcontext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
TelephonyManager mTelephony = (TelephonyManager) mcontext
.getSystemService(Context.TELEPHONY_SERVICE);
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {// WIFI
return "WIFI";
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {// 3G
return "3G";
} else {
return "2G";
}
}
return null;
}
/** 获取当前设备的SN */
public static String getSimSN(Context context) {
if (null == context) {
return null;
}
String simSN = null;
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
simSN = tm.getSimSerialNumber();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return simSN;
}
/** 获取当前设备的MAC地址 */
public static String getMacAddress(Context context) {
if (null == context) {
return null;
}
String mac = null;
try {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wm.getConnectionInfo();
mac = info.getMacAddress();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return mac;
}
/** 获得设备ip地址 */
public static String getLocalAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
Log.e(TAG,e.getMessage());
}
return null;
}
/** 获取屏幕的分辨率 */
@SuppressWarnings("deprecation")
public static int[] getResolution(Context context) {
if (null == context) {
return null;
}
WindowManager windowMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int[] res = new int[2];
res[0] = windowMgr.getDefaultDisplay().getWidth();
res[1] = windowMgr.getDefaultDisplay().getHeight();
return res;
}
/** 获得设备的横向dpi */
public static float getWidthDpi(Context context) {
if (null == context) {
return 0;
}
DisplayMetrics dm = null;
try {
if (context != null) {
dm = new DisplayMetrics();
dm = context.getApplicationContext().getResources().getDisplayMetrics();
}
return dm.densityDpi;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/** 获得设备的纵向dpi */
public static float getHeightDpi(Context context) {
if (null == context) {
return 0;
}
DisplayMetrics dm = new DisplayMetrics();
dm = context.getApplicationContext().getResources().getDisplayMetrics();
return dm.ydpi;
}
/** 获取设备信息 */
public static String[] getDivceInfo() {
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) {
Log.e(TAG,e.getMessage());
}
return cpuInfo;
}
/** 判断手机CPU是否支持NEON指令集 */
public static boolean isNEON() {
boolean isNEON = false;
String cupinfo = getCPUInfos();
if (cupinfo != null) {
cupinfo = cupinfo.toLowerCase();
isNEON = cupinfo != null && cupinfo.contains("neon");
}
return isNEON;
}
/** 读取CPU信息文件,获取CPU信息 */
@SuppressWarnings("resource")
private static String getCPUInfos() {
String str1 = "/proc/cpuinfo";
String str2 = "";
StringBuilder resusl = new StringBuilder();
String resualStr = null;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
resusl.append(str2);
// String cup = str2;
}
if (resusl != null) {
resualStr = resusl.toString();
return resualStr;
}
} catch (IOException e) {
Log.e(TAG,e.getMessage());
}
return resualStr;
}
/** 获取当前设备cpu的型号 */
public static int getCPUModel() {
return matchABI(getSystemProperty("ro.product.cpu.abi")) | matchABI(getSystemProperty("ro.product.cpu.abi2"));
}
/** 匹配当前设备的cpu型号 */
private static int matchABI(String abiString) {
if (TextUtils.isEmpty(abiString)) {
return 0;
}
if ("armeabi".equals(abiString)) {
return 1;
} else if ("armeabi-v7a".equals(abiString)) {
return 2;
} else if ("x86".equals(abiString)) {
return 4;
} else if ("mips".equals(abiString)) {
return 8;
}
return 0;
}
/** 获取CPU核心数 */
public static int getCpuCount() {
return Runtime.getRuntime().availableProcessors();
}
/** 获取Rom版本 */
public static String getRomversion() {
String rom = "";
try {
String modversion = getSystemProperty("ro.modversion");
String displayId = getSystemProperty("ro.build.display.id");
if (modversion != null && !modversion.equals("")) {
rom = modversion;
}
if (displayId != null && !displayId.equals("")) {
rom = displayId;
}
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return rom;
}
/** 获取系统配置参数 */
public static String getSystemProperty(String key) {
String pValue = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method m = c.getMethod("get", String.class);
pValue = m.invoke(null, key).toString();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return pValue;
}
/** 获取系统中的Library包 */
public static List<String> getSystemLibs(Context context) {
if (null == context) {
return null;
}
PackageManager pm = context.getPackageManager();
String[] libNames = pm.getSystemSharedLibraryNames();
List<String> listLibNames = Arrays.asList(libNames);
Log.e(TAG,"SystemLibs: " + listLibNames);
return listLibNames;
}
/** 获取手机外部可用空间大小,单位为byte */
@SuppressWarnings("deprecation")
public static long getExternalTotalSpace() {
long totalSpace = -1L;
if (FileUtil.isSDCardAvailable()) {
try {
String path = Environment.getExternalStorageDirectory().getPath();// 获取外部存储目录即 SDCard
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
totalSpace = totalBlocks * blockSize;
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
}
return totalSpace;
}
/** 获取外部存储可用空间,单位为byte */
@SuppressWarnings("deprecation")
public static long getExternalSpace() {
long availableSpace = -1L;
if (FileUtil.isSDCardAvailable()) {
try {
String path = Environment.getExternalStorageDirectory().getPath();
StatFs stat = new StatFs(path);
availableSpace = stat.getAvailableBlocks() * (long) stat.getBlockSize();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
}
return availableSpace;
}
/** 获取手机内部空间大小,单位为byte */
@SuppressWarnings("deprecation")
public static long getTotalInternalSpace() {
long totalSpace = -1L;
try {
String path = Environment.getDataDirectory().getPath();
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();// 获取该区域可用的文件系统数
totalSpace = totalBlocks * blockSize;
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return totalSpace;
}
/** 获取手机内部可用空间大小,单位为byte */
@SuppressWarnings("deprecation")
public static long getAvailableInternalMemorySize() {
long availableSpace = -1l;
try {
String path = Environment.getDataDirectory().getPath();// 获取 Android 数据目录
StatFs stat = new StatFs(path);// 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况
long blockSize = stat.getBlockSize();// 返回 Int ,大小,以字节为单位,一个文件系统
long availableBlocks = stat.getAvailableBlocks();// 返回 Int ,获取当前可用的存储空间
availableSpace = availableBlocks * blockSize;
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return availableSpace;
}
/** 获取单个应用最大分配内存,单位为byte */
public static long getOneAppMaxMemory(Context context) {
if (context == null) {
return -1;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return activityManager.getMemoryClass() * 1024 * 1024;
}
/** 获取指定本应用占用的内存,单位为byte */
public static long getUsedMemory(Context context) {
return getUsedMemory(context,null);
}
/** 获取指定包名应用占用的内存,单位为byte */
public static long getUsedMemory(Context context,String packageName) {
if (context == null) {
return -1;
}
if (StringUtil.isEmpty(packageName)) {
packageName = context.getPackageName();
}
long size = 0;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runapps = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo runapp : runapps) { // 遍历运行中的程序
if (packageName.equals(runapp.processName)) {// 得到程序进程名,进程名一般就是包名,但有些程序的进程名并不对应一个包名
// 返回指定PID程序的内存信息,可以传递多个PID,返回的也是数组型的信息
Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(new int[]{runapp.pid});
// 得到内存信息中已使用的内存,单位是K
size = processMemoryInfo[0].getTotalPrivateDirty() * 1024;
}
}
return size;
}
/** 获取手机剩余内存,单位为byte */
public static long getAvailableMemory(Context context) {
if (context == null) {
return -1;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
return info.availMem;
}
/** 获取手机总内存,单位为byte */
public static long getTotalMemory() {
long size = 0;
String path = "/proc/meminfo";// 系统内存信息文件
try {
String totalMemory = FileUtil.readProperties(path, "MemTotal", null);// 读出来是带单位kb的,并且单位前有空格,所以去掉最后三位
if (!StringUtil.isEmpty(totalMemory) && totalMemory.length() > 3) {
size = Long.valueOf(totalMemory.substring(0, totalMemory.length() - 3)) * 1024;
}
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
return size;
}
/** 手机低内存运行阀值,单位为byte */
public static long getThresholdMemory(Context context) {
if (context == null) {
return -1;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
return info.threshold;
}
/** 手机是否处于低内存运行 */
public static boolean isLowMemory(Context context) {
if (context == null) {
return false;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
return info.lowMemory;
}
}