Created
November 3, 2012 21:40
-
-
Save kristopherjohnson/4008947 to your computer and use it in GitHub Desktop.
Get unique identifier for an Android device
This file contains 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
public class DeviceInfo { | |
private static final String PREFERENCES = "DeviceInfo.PREFERENCES"; | |
private static final String KEY_DEVICEID = "DeviceId"; | |
/** | |
* @return unique identifier string for the device | |
*/ | |
public static synchronized String getDeviceId(Context context) { | |
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, 0); | |
String value = prefs.getString(KEY_DEVICEID, null); | |
if (value == null) { | |
value = UUID.randomUUID().toString(); | |
prefs.edit().putString(KEY_DEVICEID, value).commit(); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment