{"id":6149,"date":"2020-08-05T17:22:06","date_gmt":"2020-08-05T11:52:06","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=6149"},"modified":"2020-08-06T14:41:09","modified_gmt":"2020-08-06T09:11:09","slug":"design-pattern-getting-a-unique-id-for-android-device","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/","title":{"rendered":"Design Pattern &#8211; Getting a Unique Id for Android Device"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">It sometimes becomes necessary to identify an Android Device depending on our use case or application requirements. There are some use cases where we might need device identification.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, when you want to :<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1-Identify an Android user to store games scores on a server<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2-Track apps installation<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are several types of methods that exist to uniquely identify an Android Device like through\u00a0 Mac Address, Unique Telephony Number (IMEI, MEID, ESN, IMSI), Build.SERIAL, UUI, ANDROID_ID, etc, but over the time most of them have been put under restriction by the Android team for security reasons.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Two recommended ways are &#8211;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1-Android Id<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2- UUID<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Android Id<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">It gets randomly generated and stored on a device first boot, This value is available via Settings.Secure.ANDROID_ID. It is a 64-bit number that remains constant for the lifetime of a device. ANDROID_ID seems a good choice for a unique device identifier because it is available for smartphones and tablets. To retrieve the value, use\u00a0 the following code :<\/span><\/p>\n<pre class=\"lang:default decode:true \">String androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Or\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true \">override fun getDeviceId(): String = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID )<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Limitations &#8212;\u00a0 Android id value may change if a device gets rooted or factory reset.<\/span><\/p>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> UUID<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">If our requirement is to identify a particular installation and not a physical device, a good solution to get a unique id for a user is to use the UUID class of Java. We can get the UUID and store it in our application using Shared Preference or any other mechanism for a particular installation.<\/span><\/p>\n<pre class=\"lang:default decode:true \">private static String uniqueID = null;\r\n\r\nprivate static final String PREF_UNIQUE_ID = \"PREF_UNIQUE_ID\";\r\n\r\npublic synchronized static String id(Context context) {\r\n\r\n\u00a0\u00a0\u00a0if (uniqueID == null) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SharedPreferences sharedPrefs = context.getSharedPreferences(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PREF_UNIQUE_ID, Context.MODE_PRIVATE);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (uniqueID == null) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uniqueID = UUID.randomUUID().toString();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Editor editor = sharedPrefs.edit();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0editor.putString(PREF_UNIQUE_ID, uniqueID);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0editor.commit();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/or\r\n\u00a0editor.apply();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0return uniqueID;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">UUID.randomUUID() method generates a unique identifier for a specific installation.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It sometimes becomes necessary to identify an Android Device depending on our use case or application requirements. There are some use cases where we might need device identification. For example, when you want to : 1-Identify an Android user to store games scores on a server 2-Track apps installation There are several types of methods [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,71],"tags":[],"class_list":["post-6149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mobile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"It sometimes becomes necessary to identify an Android Device depending on our use case or application requirements. There are some use cases where we might need device identification. For example, when you want to : 1-Identify an Android user to store games scores on a server 2-Track apps installation There are several types of methods [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-05T11:52:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T09:11:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Design Pattern &#8211; Getting a Unique Id for Android Device\",\"datePublished\":\"2020-08-05T11:52:06+00:00\",\"dateModified\":\"2020-08-06T09:11:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/\"},\"wordCount\":267,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Android-Blog-Post.png\",\"articleSection\":[\"Android\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/\",\"name\":\"Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Android-Blog-Post.png\",\"datePublished\":\"2020-08-05T11:52:06+00:00\",\"dateModified\":\"2020-08-06T09:11:09+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Android-Blog-Post.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Android-Blog-Post.png\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-getting-a-unique-id-for-android-device\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Design Pattern &#8211; Getting a Unique Id for Android Device\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/","og_locale":"en_US","og_type":"article","og_title":"Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog","og_description":"It sometimes becomes necessary to identify an Android Device depending on our use case or application requirements. There are some use cases where we might need device identification. For example, when you want to : 1-Identify an Android user to store games scores on a server 2-Track apps installation There are several types of methods [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/","og_site_name":"InnovationM - Blog","article_published_time":"2020-08-05T11:52:06+00:00","article_modified_time":"2020-08-06T09:11:09+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Design Pattern &#8211; Getting a Unique Id for Android Device","datePublished":"2020-08-05T11:52:06+00:00","dateModified":"2020-08-06T09:11:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/"},"wordCount":267,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png","articleSection":["Android","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/","url":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/","name":"Design Pattern - Getting a Unique Id for Android Device - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png","datePublished":"2020-08-05T11:52:06+00:00","dateModified":"2020-08-06T09:11:09+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2020\/08\/Android-Blog-Post.png","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-getting-a-unique-id-for-android-device\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Design Pattern &#8211; Getting a Unique Id for Android Device"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/6149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=6149"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/6149\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/6152"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=6149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=6149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=6149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}