获取设备id imei uuid mac地址 以及根据不同的id 生成uuid
2018-02-09 11:18
656 查看
package com.mny.macimeideviceinfo; import android.content.Context; import android.provider.Settings; import android.telephony.TelephonyManager; import java.net.NetworkInterface; import java.net.SocketException; /** * Created by MnyZhao on 2018/2/9. */ public class DeviceInfoUtils { public static DeviceInfoUtils deviceInfoUtils; public static DeviceInfoUtils getDeviceInfoUtils(Context context) { if (deviceInfoUtils == null) { deviceInfoUtils = new DeviceInfoUtils(); } return deviceInfoUtils; } /*Deviceid*/ public String getDeivceId(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); final String deviceId = telephonyManager.getDeviceId(); final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); String uuid = ""; if (deviceId != null) { uuid = deviceId; } else { if (!"9774d56d682e549c".equals(androidId)) { uuid = androidId; } else { // uuid = UUID.randomUUID().toString(); uuid = getUUID(context); } } return uuid; } /*Imei*/ public String getImei(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getDeviceId(); } /*imsi*/ public String getImsi(Context context){ TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getSubscriberId(); } /*wifi MAC*/ public String getMacAddress() { String macAddress = null; StringBuffer buf = new StringBuffer(); NetworkInterface networkInterface = null; try { networkInterface = NetworkInterface.getByName("eth1"); if (networkInterface == null) { networkInterface = NetworkInterface.getByName("wlan0"); } if (networkInterface == null) { return "02:00:00:00:00:02"; } byte[] addr = networkInterface.getHardwareAddress(); for (byte b : addr) { buf.append(String.format("%02X:", b)); } if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); } macAddress = buf.toString(); } catch (SocketException e) { e.printStackTrace(); return "02:00:00:00:00:02"; } return macAddress; } /*getAndroidId*/ public String getAndroidID(Context context) { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } /*getuuid*/ public String getUUID(Context context) { DeviceUuidFactory deviceUuidFacto = new DeviceUuidFactory(context); return deviceUuidFacto.getDeviceUuid().toString(); } }
<uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>package com.mny.macimeideviceinfo;import android.content.Context;import android.content.SharedPreferences;import android.provider.Settings;import android.telephony.TelephonyManager;import java.io.UnsupportedEncodingException;import java.util.UUID;public class DeviceUuidFactory {protected static final String PREFS_FILE = "device_id.xml";protected static final String PREFS_DEVICE_ID = "device_id";protected static volatile UUID uuid;public DeviceUuidFactory(Context context) {if (uuid == null) {synchronized (DeviceUuidFactory.class) {if (uuid == null) {final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);final String id = prefs.getString(PREFS_DEVICE_ID, null);if (id != null) {// Use the ids previously computed and stored in the// prefs fileuuid = UUID.fromString(id);} else {final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);// Use the Android ID unless it's broken, in which case// fallback on deviceId,// unless it's not available, then fallback on a random// number which we store to a prefs filetry {if (!"9774d56d682e549c".equals(androidId)) {uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));} else {TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);final String deviceId = telephonyManager.getDeviceId();uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();}} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}// Write the value out to the prefs fileprefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();}}}}}/*** Returns a unique UUID for the current android device. As with all UUIDs,* this unique ID is "very highly likely" to be unique across all Android* devices. Much more so than ANDROID_ID is.* <p>* The UUID is generated by using ANDROID_ID as the base key if appropriate,* falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to* be incorrect, and finally falling back on a random UUID that's persisted* to SharedPreferences if getDeviceID() does not return a usable value.* <p>* In some rare circumstances, this ID may change. In particular, if the* device is factory reset a new device ID may be generated. In addition, if* a user upgrades their phone from certain buggy implementations of Android* 2.2 to a newer, non-buggy version of Android, the device ID may change.* Or, if a user uninstalls your app on a device that has neither a proper* Android ID nor a Device ID, this ID may change on reinstallation.* <p>* Note that if the code falls back on using TelephonyManager.getDeviceId(),* the resulting ID will NOT change after a factory reset. Something to be* aware of.* <p>* Works around a bug in Android 2.2 for many devices when using ANDROID_ID* directly.** @return a UUID that may be used to uniquely identify your device for most* purposes.* @see http://code.google.com/p/android/issues/detail?id=10603*/public UUID getDeviceUuid() {return uuid;}}
相关文章推荐
- Android UUID生成(IMEI、设备序列号、MAC地址)
- 根据mac地址+deviceid获取设备唯一编码 获取手机及SIM卡相关信息
- js如何根据id获取标签内的值,以及如何通过id赋值
- 如何获取iphone\ipad的UUID(设备ID)号?
- Android获取设备IMEI和Mac地址
- 怎样获取iPhone/ipad的设备ID(UUID)号?
- 获取IMEI设备的唯一标识以及异常提交
- 获取手机(ios,android)的设备唯一码(mac地址, IMEI)
- java获取硬盘ID以及MAC地址等唯一标识码
- java如何根据list以及根节点id获取树形展示数据?
- Android 获取CellId以及IMEI 获取基站id
- 嵌入式 获取网卡设备以及IP和MAC地址示例以及网络编程之ioctl小结
- magento中根据用户的id获取用户的所有订单以及每个订单中的物品 以及物品的相关属性
- Hibernate注解使用以及Spring整合 , hibernate 自动生成uuid类型的id 注解
- 微信蓝牙设备开发--添加设备以及获取微信为设备生成的二维码
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
- Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
- 根据不同的实体及其ID来获取数据库中的数据
- c++通过mac地址 获取 设备UUID