Skip to content

Commit ef1fb15

Browse files
xiangqing.xxqBlankj
authored andcommitted
添加设备信息获取方法
1 parent 130b4bc commit ef1fb15

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

utilcode/lib/src/main/java/com/blankj/utilcode/util/DeviceUtils.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import android.provider.Settings;
1111
import android.support.annotation.RequiresApi;
1212
import android.support.annotation.RequiresPermission;
13+
import android.telecom.TelecomManager;
14+
import android.telephony.TelephonyManager;
1315
import android.text.TextUtils;
1416

1517
import java.io.File;
@@ -20,6 +22,7 @@
2022

2123
import static android.Manifest.permission.ACCESS_WIFI_STATE;
2224
import static android.Manifest.permission.INTERNET;
25+
import static android.Manifest.permission.READ_PHONE_STATE;
2326

2427
/**
2528
* <pre>
@@ -43,7 +46,8 @@ private DeviceUtils() {
4346
public static boolean isDeviceRooted() {
4447
String su = "su";
4548
String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/",
46-
"/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/"};
49+
"/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/",
50+
"/system/sbin/", "/usr/bin/", "/vendor/bin/"};
4751
for (String location : locations) {
4852
if (new File(location + su).exists()) {
4953
return true;
@@ -65,6 +69,48 @@ public static boolean isAdbEnabled() {
6569
) > 0;
6670
}
6771

72+
/**
73+
* Return the imei of device.
74+
*
75+
* @return the imei of device
76+
*/
77+
@SuppressLint("HardwareIds")
78+
@RequiresPermission(value = READ_PHONE_STATE)
79+
public static String getImei() {
80+
String imei = "";
81+
try {
82+
TelephonyManager telephonyMgr = (TelephonyManager) Utils.getApp()
83+
.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
84+
if (telephonyMgr != null) {
85+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
86+
imei = telephonyMgr.getImei();
87+
} else {
88+
imei = telephonyMgr.getDeviceId();
89+
}
90+
}
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
return imei;
95+
}
96+
97+
/**
98+
* Return the serial of device.
99+
*
100+
* @return the serial of device
101+
*/
102+
@SuppressLint("HardwareIds")
103+
@RequiresPermission(value = READ_PHONE_STATE)
104+
public static String getSerial() {
105+
String serial = "";
106+
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
107+
serial = Build.getSerial();
108+
} else {
109+
serial = Build.SERIAL;
110+
}
111+
return serial;
112+
}
113+
68114
/**
69115
* Return the version name of device's system.
70116
*

utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/device/DeviceActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class DeviceActivity : BaseBackActivity() {
5252
appendLine("isAdbEnabled: " + DeviceUtils.isAdbEnabled())
5353
}
5454
}
55+
.appendLine("getImei: " + DeviceUtils.getImei())
56+
.appendLine("getSerial: " + DeviceUtils.getSerial())
5557
.appendLine("getSDKVersionName: " + DeviceUtils.getSDKVersionName())
5658
.appendLine("getSDKVersionCode: " + DeviceUtils.getSDKVersionCode())
5759
.appendLine("getAndroidID: " + DeviceUtils.getAndroidID())

0 commit comments

Comments
 (0)