11

I want to allocate unique ID to each user as soon as he installs the application so that whenever the app contacts the server I know who is contacting. For this purpose, I thought that on first time installation, the app contacts the server and gets unique ID. But I don't know where to store it permanently so that next time when app is started, it knows what its ID is rather than contacting server.

Sorry if that is some obvious question as I am newbie.

Gaurav
  • 1,005
  • 3
  • 14
  • 33
  • 1
    Is there a reason you're not going for username/password? If a user has multiple devices and/or replaces their Android device, you won't be able to know it's them anymore. (not saying it's not the right thing to do in this case, but what _is_ this case really? :) – Joachim Isaksson Jan 29 '12 at 06:38
  • 1
    Btw, out of all the duplicates about unique IDs, that seems to be one of the most authoritative ones: http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id, although that doesn't consider the caveats I mentioned in my answer. – EboMike Jan 29 '12 at 07:05
  • I want to track user's activity and send it to server. So whenever a customer enters that unique ID into my website, he would be able to see the logs associated with that user. That is why I don't want him to have login ID as app would always be running in background. – Gaurav Jan 29 '12 at 09:06
  • The problem is duplicate, Try this solution: https://stackoverflow.com/a/49400168/907233 – Bingerz Mar 21 '18 at 07:11

4 Answers4

9

This question has been asked many times on Stack Overflow.

In short: Android has always supported a unique ID. However, prior to Android 2.2, the ID was not always identical on certain kinds of phones. Since 2.2 is pretty ubiquitous by now, I would use that ID.

The Android Developer Blog has a good article about this.

And as Joachim said - you may want to consider a different approach altogether. Android's unique ID is good and persistent across factory resets, but not across a device upgrade. Also keep in mind that many people have several devices (like a phone and a tablet). You may want to use the Google account instead, the AccountManager can help you there.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • How do you add security to this ? if a malicious app is installed on this phone and it can read this android_id ..it can easily start calling my api with this id so i introduced an auth token ... but i need help figuring out how to ensure that i generate a password which only my app on this device can generate and i can verify it on the server side.. hardcoding a key in the code is bad practice – Anurag Dalia Nov 04 '19 at 19:56
2

Use SharedPreferences to store the unique id.

Here is an example:

Android SharedPreferences

For more complex data, you can use SQlite.

Community
  • 1
  • 1
Guillermo Tobar
  • 344
  • 4
  • 17
  • I am thinking of generating random ID on server using user's Device ID or some other ID which I'll figure out soon. Problem was to store random ID on device. So problem solved. Thanks. :) – Gaurav Jan 29 '12 at 09:58
  • SharedPreferences won't survive reinstall or factory reset or simply clear app data operation by the user. You might want to back it up on user's google account but it still can be deleted easily. – A.J. Dec 20 '14 at 18:44
1

For unique id, you can use IMEI of device on which application is going to install. Refer this link for how to get IMEI number. Then stored that IMEI number in shared preference. Refer Guillermo lobar's link for that. You need to check for that unique id in preference when you application starts. At very first time, save that in preference. So when next time it checks for that id, app find it in preference and hence no need to connecting server. :)

Community
  • 1
  • 1
Ravi Bhatt
  • 1,930
  • 1
  • 14
  • 27
  • @EboMike Thanks to make me to take look on that. – Ravi Bhatt Jan 29 '12 at 09:03
  • @Gaurav In case of tablet, Only 3G Tablets have IMEI. But by the link I have given in my answer, you can find the deviceId of tablet too and which is unique. And for unique ID you can use ANDROID_ID too. Refer [http://stackoverflow.com/questions/8366013/android-imei-number-for-tablet]. But it is not much reliable as per the answer of this link. – Ravi Bhatt Jan 29 '12 at 09:09
  • The target users of app are only mobile users. So I think it may help. – Gaurav Jan 29 '12 at 09:50
  • IMEI is usually good, but not always: http://android-developers.blogspot.co.il/2011/03/identifying-app-installations.html – android developer Feb 27 '15 at 22:59
0

You could get the IMEI of the device. As of API 26, getDeviceId() is deprecated. If you need to get the IMEI of the device, use the following:

 String deviceId = "";
    if (Build.VERSION.SDK_INT >= 26) {
        deviceId = getSystemService(TelephonyManager.class).getImei();
    }else{
        deviceId = getSystemService(TelephonyManager.class).getDeviceId();
    }
DummyData
  • 645
  • 9
  • 14