Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created November 3, 2012 21:40
Show Gist options
  • Save kristopherjohnson/4008947 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/4008947 to your computer and use it in GitHub Desktop.
Get unique identifier for an Android device
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