Skip to content

Instantly share code, notes, and snippets.

@crgarridos
Created August 8, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crgarridos/455a004770886cfe26acb7fdc64f019d to your computer and use it in GitHub Desktop.
Save crgarridos/455a004770886cfe26acb7fdc64f019d to your computer and use it in GitHub Desktop.
@RequiresPermission("android.permission.READ_PHONE_STATE")
public static String getDeviceId(Context context) {
String androidId = Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
if (androidId != null)
return androidId;
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
if (tm.getDeviceId() != null)
return tm.getDeviceId();
else
return tm.getSimSerialNumber();
} else
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment