我想吃火锅
我想吃火锅

Reputation: 19

How to get unique device numer in Android?

I have tried to these methods such as IMEI,MEID,mac address,android_id, but all not OK.

How to get unique device numer in Android?

Upvotes: 0

Views: 12611

Answers (5)

Daniel  Luche
Daniel Luche

Reputation: 161

In Android 10 you can't have access to non resettable device ids, like IMEI ,MEID etc.

So I think your best chance is creating a UUID. You can do that using the code below and saving this UUID into database or preference file.

   String uniqueID = UUID.randomUUID().toString();

As a random number, everytime you call this method it'll return a different code, and I think is not you are looking for.

To get a "unique ID" from UUID, the best choise is the code below.It will work fine, but the documentation ask to avoid using Android_ID too.

   String androidId = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);

   UUID androidId_UUID = UUID
            .nameUUIDFromBytes(androidId.getBytes("utf8"));

This code will give you a almost unique code because if the user do a factory reset, this number would be changed.

I'm looking for another way to get a unique ID but, until now, I couldn't find.

Here ar some Android Developers documentation that should be usefull. Good pratices to define a unique ID https://developer.android.com/training/articles/user-data-ids

How to define a unique Id based in you use case:

https://developer.android.com/training/articles/user-data-ids#common-use-cases

I hope this could help someone.

Upvotes: 2

S Kranthi Kumar
S Kranthi Kumar

Reputation: 750

Below code is I used in my project

To get Unique Android Id

 static String androidID = Settings.Secure.getString(MyApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);

I used this as a global so i can use anywhere in my project

Upvotes: 1

Anmol
Anmol

Reputation: 568

As the requirement for most of the applications is to identify a particular installation and not a physical device, a good solution to get the unique id for a user if to use UUID class. The following solution has been presented by Reto Meier from Google in a Google I/O presentation :

private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
public synchronized static String id(Context context) {
   if (uniqueID == null) {
      SharedPreferences sharedPrefs = context.getSharedPreferences(
         PREF_UNIQUE_ID, Context.MODE_PRIVATE);
      uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
      if (uniqueID == null) {
         uniqueID = UUID.randomUUID().toString();
         Editor editor = sharedPrefs.edit();
     editor.putString(PREF_UNIQUE_ID, uniqueID);
     editor.commit();
     }
   }
   return uniqueID;
}

UUID.randomUUID() method generates an unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application. You can also try to associate this solution with Android Backup service to keep the information available to the user even if he installs your application on the other device.

You can explore more at https://medium.com/@ssaurel/how-to-retrieve-an-unique-id-to-identify-android-devices-6f99fd5369eb

Upvotes: 2

Android Geek
Android Geek

Reputation: 9225

To retrieve the unique ID associated to your device, you can use the following code :

import android.telephony.TelephonyManager;
import android.content.Context;
// ...
 TelephonyManager telephonyManager;
  telephonyManager = (TelephonyManager) getSystemService(Context.
                TELEPHONY_SERVICE);
        /*
      * getDeviceId() returns the unique device ID.
       * For example,the IMEI for GSM and the MEID or ESN for CDMA phones.
         */
            String deviceId = telephonyManager.getDeviceId();
         /*
       * getSubscriberId() returns the unique subscriber ID,
         * For example, the IMSI for a GSM phone.
          */
           String subscriberId = telephonyManager.getSubscriberId();

Secure Android ID

String androidId = Settings.Secure.getString(getContentResolver(),
                 Settings.Secure.ANDROID_ID);

Upvotes: 0

Venki WAR
Venki WAR

Reputation: 2027

There are several Unique Identifiers available for Android devices

IMEI

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei = TelephonyMgr.getDeviceId();

permission

android.permission.READ_PHONE_STATE

WLAN MAC Address

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String wanMAC = wifi .getConnectionInfo().getMacAddress();

permission

android.permission.ACCESS_WIFI_STATE

Bluetooth MAC Address

BluetoothAdapter bluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
String bluetoothMAC = bluetoothAdapter .getAddress();

permission

android.permission.BLUETOOTH

Android ID

String androidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

Upvotes: 4

Related Questions