|
1 | 1 | package com.androidex.utils;
|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
| 4 | +import android.net.wifi.WifiInfo; |
| 5 | +import android.net.wifi.WifiManager; |
4 | 6 | import android.provider.Settings;
|
5 | 7 | import android.telephony.TelephonyManager;
|
6 |
| -import android.text.*; |
7 | 8 |
|
8 | 9 | import com.androidex.context.ExApplication;
|
9 | 10 |
|
| 11 | +import java.io.File; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.RandomAccessFile; |
10 | 15 | import java.net.Inet4Address;
|
11 | 16 | import java.net.InetAddress;
|
12 | 17 | import java.net.NetworkInterface;
|
13 | 18 | import java.util.Enumeration;
|
| 19 | +import java.util.UUID; |
14 | 20 |
|
15 | 21 | /**
|
16 | 22 | * 设备硬件相关信息
|
17 | 23 | */
|
18 | 24 | public class DeviceUtil {
|
19 | 25 |
|
20 |
| - /** |
21 |
| - * 判断是否有电话功能 |
22 |
| - * |
23 |
| - * @return true:有电话功能 |
24 |
| - * false:没有电话功能 |
25 |
| - */ |
26 |
| - public static boolean hasPhone() { |
27 |
| - |
28 |
| - TelephonyManager telephony = (TelephonyManager) ExApplication.getContext(). |
29 |
| - getSystemService(Context.TELEPHONY_SERVICE); |
30 |
| - return telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE ? false : true; |
31 |
| - } |
32 |
| - |
33 |
| - /** |
34 |
| - * 获取手机的IMEI |
35 |
| - */ |
36 |
| - public static String getIMEI() { |
37 |
| - |
38 |
| - String imei = ""; |
39 |
| - try { |
40 |
| - Context ctx = ExApplication.getContext(); |
41 |
| - TelephonyManager telephonyManager = |
42 |
| - (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); |
43 |
| - if (telephonyManager != null) { |
44 |
| - |
45 |
| - imei = telephonyManager.getDeviceId(); |
46 |
| - if (android.text.TextUtils.isEmpty(imei)) { |
47 |
| - imei = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID); |
| 26 | + private static String sID = null; |
| 27 | + private static final String INSTALLATION = "INSTALLATION"; |
| 28 | + |
| 29 | + /** |
| 30 | + * 判断是否有电话功能 |
| 31 | + * android.permission.READ_PHONE_STATE |
| 32 | + * |
| 33 | + * @return true:有电话功能 |
| 34 | + * false:没有电话功能 |
| 35 | + */ |
| 36 | + public static boolean hasPhone() { |
| 37 | + |
| 38 | + TelephonyManager telephony = (TelephonyManager) ExApplication.getContext(). |
| 39 | + getSystemService(Context.TELEPHONY_SERVICE); |
| 40 | + return telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE ? false : true; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * 获取手机的IMEI |
| 45 | + * android.permission.READ_PHONE_STATE |
| 46 | + * 1)非手机设备: 如果只带有Wifi的设备或者音乐播放器没有通话的硬件功能的话就没有这个DEVICE_ID |
| 47 | + * 2)权限: 获取DEVICE_ID需要READ_PHONE_STATE权限,但如果我们只为了获取它,没有用到其他的通话功能,那这个权限有点大才小用 |
| 48 | + * 3)bug:在少数的一些手机设备上,该实现有漏洞,会返回垃圾,如:zeros(全是0)或者asterisks(星号)的产品 |
| 49 | + */ |
| 50 | + public static String getIMEI() { |
| 51 | + |
| 52 | + String imei = ""; |
| 53 | + try { |
| 54 | + Context ctx = ExApplication.getContext(); |
| 55 | + TelephonyManager telephonyManager = |
| 56 | + (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); |
| 57 | + if (telephonyManager != null) { |
| 58 | + |
| 59 | + imei = telephonyManager.getDeviceId(); |
| 60 | + if (imei == null) imei = ""; |
| 61 | + } |
| 62 | + } catch (Exception e) { |
| 63 | + e.printStackTrace(); |
48 | 64 | }
|
49 | 65 |
|
50 |
| - if (imei == null) imei = ""; |
51 |
| - } |
52 |
| - } catch (Exception e) { |
53 |
| - e.printStackTrace(); |
| 66 | + return imei; |
54 | 67 | }
|
55 | 68 |
|
56 |
| - return imei; |
57 |
| - } |
58 |
| - |
59 |
| - /** |
60 |
| - * 获取手机IP地址 |
61 |
| - */ |
62 |
| - private String getPhoneIP() { |
63 |
| - try { |
64 |
| - for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); |
65 |
| - en.hasMoreElements(); ) { |
66 |
| - NetworkInterface intf = en.nextElement(); |
67 |
| - for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); |
68 |
| - enumIpAddr.hasMoreElements(); ) { |
69 |
| - InetAddress inetAddress = enumIpAddr.nextElement(); |
70 |
| - if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { |
71 |
| - return inetAddress.getHostAddress().toString(); |
72 |
| - } |
| 69 | + |
| 70 | + /** |
| 71 | + * ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置ANDROID_ID似乎是获取Device ID的一个好选择, |
| 72 | + * <p> |
| 73 | + * 但它也有缺陷: |
| 74 | + * 1)它在Android <=2.1 or Android >=2.3的版本是可靠、稳定的,但在2.2的版本并不是100%可靠的 |
| 75 | + * 2)在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:9774d56d682e549c |
| 76 | + * 3)厂商定制系统的Bug:有些设备返回的值为null。 |
| 77 | + * 4)设备差异:对于CDMA设备,ANDROID_ID和TelephonyManager.getDeviceId() 返回相同的值。 |
| 78 | + * |
| 79 | + * @return |
| 80 | + */ |
| 81 | + public static String getAndroidId() { |
| 82 | + |
| 83 | + String androidId = ""; |
| 84 | + try { |
| 85 | + Context ctx = ExApplication.getContext(); |
| 86 | + androidId = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID); |
| 87 | + } catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + return androidId; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * 获取手机IP地址 |
| 95 | + */ |
| 96 | + public static String getPhoneIP() { |
| 97 | + try { |
| 98 | + for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); |
| 99 | + en.hasMoreElements(); ) { |
| 100 | + NetworkInterface intf = en.nextElement(); |
| 101 | + for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); |
| 102 | + enumIpAddr.hasMoreElements(); ) { |
| 103 | + InetAddress inetAddress = enumIpAddr.nextElement(); |
| 104 | + if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { |
| 105 | + return inetAddress.getHostAddress().toString(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (Exception e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + return ""; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * 获取MAC地址 |
| 118 | + * android.permission.ACCESS_WIFI_STATE |
| 119 | + * 1)硬件限制:并不是所有的设备都有Wifi和蓝牙硬件,硬件不存在自然也就得不到这一信息。 |
| 120 | + * 2)获取的限制:如果Wifi没有打开过,是无法获取其Mac地址的;而蓝牙是只有在打开的时候才能获取到其Mac地址。 |
| 121 | + * |
| 122 | + * @param mContext |
| 123 | + * @return |
| 124 | + */ |
| 125 | + public static String getMacAddress(Context mContext) { |
| 126 | + String macStr = ""; |
| 127 | + try { |
| 128 | + WifiManager wifiManager = (WifiManager) mContext |
| 129 | + .getSystemService(Context.WIFI_SERVICE); |
| 130 | + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
| 131 | + if (wifiInfo.getMacAddress() != null) { |
| 132 | + macStr = wifiInfo.getMacAddress();// MAC地址 |
| 133 | + } |
| 134 | + } catch (Exception e) { |
| 135 | + e.printStackTrace(); |
73 | 136 | }
|
74 |
| - } |
75 |
| - } catch (Exception e) { |
76 |
| - e.printStackTrace(); |
| 137 | + return macStr; |
77 | 138 | }
|
78 |
| - return ""; |
79 |
| - } |
| 139 | + |
| 140 | + /** |
| 141 | + * Sim Serial Number |
| 142 | + * 装有SIM卡的设备,可以通过下面的方法获取到Sim Serial Number: |
| 143 | + */ |
| 144 | + public static String getSimSerialNumber() { |
| 145 | + |
| 146 | + String ssNumber = ""; |
| 147 | + try { |
| 148 | + Context ctx = ExApplication.getContext(); |
| 149 | + TelephonyManager telephonyManager = |
| 150 | + (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); |
| 151 | + if (telephonyManager != null) { |
| 152 | + |
| 153 | + ssNumber = telephonyManager.getSimSerialNumber(); |
| 154 | + if (ssNumber == null) ssNumber = ""; |
| 155 | + } |
| 156 | + } catch (Exception e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
| 159 | + return ssNumber; |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + /** |
| 164 | + * 获取设备的UUID |
| 165 | + * |
| 166 | + * @param context |
| 167 | + * @return |
| 168 | + */ |
| 169 | + public synchronized static String getUUID(Context context) { |
| 170 | + if (sID == null) { |
| 171 | + File installation = new File(context.getFilesDir(), INSTALLATION); |
| 172 | + try { |
| 173 | + if (!installation.exists()) writeInstallationFile(installation); |
| 174 | + sID = readInstallationFile(installation); |
| 175 | + } catch (Exception e) { |
| 176 | + throw new RuntimeException(e); |
| 177 | + } |
| 178 | + } |
| 179 | + return sID; |
| 180 | + } |
| 181 | + |
| 182 | + private static String readInstallationFile(File installation) throws IOException { |
| 183 | + RandomAccessFile f = new RandomAccessFile(installation, "r"); |
| 184 | + byte[] bytes = new byte[(int) f.length()]; |
| 185 | + f.readFully(bytes); |
| 186 | + f.close(); |
| 187 | + return new String(bytes); |
| 188 | + } |
| 189 | + |
| 190 | + private static void writeInstallationFile(File installation) throws IOException { |
| 191 | + FileOutputStream out = new FileOutputStream(installation); |
| 192 | + String id = UUID.randomUUID().toString(); |
| 193 | + out.write(id.getBytes()); |
| 194 | + out.close(); |
| 195 | + } |
| 196 | + |
80 | 197 | }
|
0 commit comments