主题:
1. 取得设备的MAC address, 如果用户没有通过wifi连网路的话,就无法取得。
2. 使用TelephonyManager的getDeviceId()。3. 另外还有一个android系统的唯一区分ANDROID_ID。
具体实现:
1,本文不概述
WifiManager wm = (WifiManager)Ctxt.getSystemService(Context.WIFI_SERVICE); return wm.getConnectionInfo().getMacAddress();
1)
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); final String tmDevice, tmSerial, androidId; tmDevice = "" + tm.getDeviceId(); tmSerial = "" + tm.getSimSerialNumber(); androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode()); String deviceId = deviceUuid.toString();might result in something like: 00000000-54b3-e7c7-0000-000046bffd97,
require permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
2)
import android.content.Context; import android.content.SharedPreferences; import android.provider.Settings.Secure; 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 volatile static 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 file uuid = UUID.fromString(id); } else { final String androidId = Secure.getString(context.getContentResolver(), 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 file try { if (!"9774d56d682e549c".equals(androidId)) { uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); } else { final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).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 file prefs.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. * * 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. * * 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. * * 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. * * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly. * * @see http://code.google.com/p/android/issues/detail?id=10603 * * @return a UUID that may be used to uniquely identify your device for most purposes. */ public UUID getDeviceUuid() { return uuid; } }
private static String uniqueID = null; private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID"; public synchronized static String id(Context context) { if (uniqueID == null) { SharedPreferences sharedPrefs = context.getSharedPreferences( PREF_UNIQUE_ID, Context.MODE_PRIVATE); uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null); if (uniqueID == null) { uniqueID = UUID.randomUUID().toString(); Editor editor = sharedPrefs.edit(); editor.putString(PREF_UNIQUE_ID, uniqueID); editor.commit(); } } return uniqueID; }
3,
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
其它方法:
1. IMEI (only for Android devices with Phone use; needs android.permission.READ_PHONE_STATE)
2. Pseudo-Unique ID (for all Android devices)
3. Android ID (can be null, can change upon factory reset, can be altered on rooted phone)
4. WLAN MAC Address string (needs android.permission.ACCESS_WIFI_STATE)
5. BT MAC Address string (devices with Bluetooth, needs android.permission.BLUETOOTH)
6.通过隐藏api,serial串号
String serial = null; try { Class<?> c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); serial = (String) get.invoke(c, "ro.serialno"); } catch (Exception ignored) { }
7,下面这个方法比较保险,不需要readphone权限
String m_szDevIDShort = "35" + //we make this look like a valid IMEI Build.BOARD.length()%10+ Build.BRAND.length()%10 + Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + Build.DISPLAY.length()%10 + Build.HOST.length()%10 + Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + Build.TAGS.length()%10 + Build.TYPE.length()%10 + Build.USER.length()%10 ; //13 digits