Last active
November 12, 2018 01:34
-
-
Save vuhung3990/0fdffcd745386e85948d to your computer and use it in GitHub Desktop.
get unique device id android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* get unique device id, reuire permission READ_PHONE_STATE | |
* | |
* @param context | |
* @return unique id string | |
*/ | |
public static String getDeviceId(Context context) { | |
String deviceID = null; | |
// TODO: check from pref if null -> generate | |
String PREFERENCE_FILE_NAME = "preference_deviceXXX"; | |
String KEY_PREF = "uiid"; | |
// don't generate uiid if exist in preference | |
String saved_id = (String) getPreferenceValue(context, PREFERENCE_FILE_NAME, KEY_PREF); | |
if (saved_id == null || saved_id == "") { | |
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
String id = telephonyManager.getDeviceId(); | |
// tablet device will return null | |
if (id == null || id == "") { | |
// maybe change when factory reset | |
String id1 = Settings.Secure.ANDROID_ID; | |
if (id1 == null || id1 == "") { | |
// last solution is using random string | |
deviceID = UUID.randomUUID().toString(); | |
} else { | |
deviceID = id1; | |
} | |
} else { | |
deviceID = id; | |
} | |
// after generate uiid, save for use later | |
writePreference(context, PREFERENCE_FILE_NAME, KEY_PREF, deviceID); | |
} else { | |
deviceID = saved_id; | |
} | |
return deviceID; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* get device id from base64 wifi Mac or android_id | |
* @param context {@link android.content.Context} | |
* @return DeviceID unique android id | |
*/ | |
public static String getDeviceID(Context context) { | |
SharedPreferences shapref = context.getSharedPreferences(AppConfig.PREFERENCE_NAME, Context.MODE_PRIVATE); | |
// get device id if available | |
String deviceID = shapref.getString(AppConfig.KEY_PREF_DEVICE_ID, null); | |
if(TextUtils.isEmpty(deviceID)){ | |
SharedPreferences.Editor editor = shapref.edit(); | |
// init device id mac wifi (nor android_id) | |
WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); | |
String macAddress = null; | |
if(wimanager.isWifiEnabled()){ | |
macAddress = wimanager.getConnectionInfo().getMacAddress(); | |
} | |
if (macAddress == null) { | |
macAddress = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); | |
} | |
// base64 encode for text | |
macAddress = Base64.encodeToString(macAddress.getBytes(), Base64.NO_PADDING); | |
// save to pref for re-use | |
editor.putString(AppConfig.KEY_PREF_DEVICE_ID, macAddress); | |
editor.commit(); | |
deviceID = macAddress; | |
} | |
return deviceID.trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment