Gaurav
Gaurav

Reputation: 1045

Unique ID for each user in Android

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.

Upvotes: 11

Views: 12591

Answers (4)

DummyData
DummyData

Reputation: 682

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();
    }

Upvotes: 0

Ravi Bhatt
Ravi Bhatt

Reputation: 1948

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. :)

Upvotes: 1

Guillermo Tobar
Guillermo Tobar

Reputation: 344

Use SharedPreferences to store the unique id.

Here is an example:

Android SharedPreferences

For more complex data, you can use SQlite.

Upvotes: 2

EboMike
EboMike

Reputation: 77762

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.

Upvotes: 9

Related Questions