1

I get android id with code below:

DeviceID = Secure.getString(this.getContentResolver(),
                    Secure.ANDROID_ID);

But it changes on every update. Is it wrong way to get it or is there anything different that i can use, that never changes and stays unique for same device everytime?

Nikoloz14
  • 247
  • 5
  • 13

3 Answers3

0

Link only answers are normally discouraged but I don't see an alternative in this case as the article is quite in depth for and relates to various different API levels.

See the documentation for the correct way to handle this (according to Google):

http://developer.android.com/training/articles/user-data-ids.html

Kuffs
  • 35,581
  • 10
  • 79
  • 92
-2

Unfortunately, there is nothing that fits your requirements.

When it comes to the unique identifiers of an Android device, there are a lot of different options, but none of them is a 100% guarantee to work. And, getting most of them require requesting nasty permissions from the user.

I can suggest two options:

  1. Using the Google Advertising ID - it's unique per device and will only change if the user manually resets it. There are no additional permissions required to extract it.
  2. You can generate a unique key per device yourself (using something like a UUID) and store it on the device (shared prefs, file, database...).
Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
  • i like second approach, but will it reset on uninstall and install? what about updatE? – Nikoloz14 Feb 03 '16 at 15:08
  • 1
    It's the downside of this approach. An uninstall will effectively wipe out the key. The thing is to evaluate if this will cause real problems in your use case. Maybe you can go around this by using this https://developers.google.com/instance-id/ – Danail Alexiev Feb 03 '16 at 15:11
-3

Use the hardwareID instead, that one shouldn't change that much...

Here's the code to try out:

/**
 * Returns the embedded <i>hardwareID</i>
 * @param context which {@link Context} to use
 * @return the hardwareID
 */
public static String getHardwareId(Context context) {
    return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
Shark
  • 6,513
  • 3
  • 28
  • 50
  • This is basically the code he is already using but wrapped in a function – Kuffs Feb 03 '16 at 15:03
  • No, it is not. Read it again please. – Shark Feb 03 '16 at 15:03
  • Settings is added. does it change much? – Nikoloz14 Feb 03 '16 at 15:03
  • It should change it much, the result you get via `Settings.Secure` it should be retained between full device reinstalls. – Shark Feb 03 '16 at 15:04
  • The code is identical. Accessing it via Settings does nothing as that is inferred anyway. http://developer.android.com/reference/android/provider/Settings.Secure.html – Kuffs Feb 03 '16 at 15:07
  • I'm not sure it's infered, but if it is - fair enough then, my bad, but if `A 64-bit number (as a hex string) that is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device. ` won't solve his problem then....... what will? – Shark Feb 03 '16 at 15:10
  • Note the word "should". He is already using the equivalent of your code and finds it insufficient for his needs. The best practice for handling this (according to Google) are given in my answer. – Kuffs Feb 03 '16 at 15:13
  • Then he's running on emulator and not a real device; hardware IDs **really** do not change that often. I still upvoted your answer @Kuffs because that's the `preferred default way of doing it`. – Shark Feb 03 '16 at 15:16
  • I agree they shouldn't change but Android is open and his device could have been modified by the manufacturer. – Kuffs Feb 03 '16 at 15:18
  • nah, i used to work for a platform vendor as an embedded engineer; rooting the device or burning new images to the device didn't change the hardwareID as much as I'd expect them to (i expected `every time`, it was more of a `every now and then` or 3/10 times on a weekly basis. So they can be used, they just can't be relied on to work 100% for the next million years. Newer Androids will probably prohibit access to them at some point... but until they do - it works and gets the job done :D – Shark Feb 03 '16 at 15:28