3

I am developing an application using api 14 (android 4.0).

in manifest:

 <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

I want to get an unique id from each device (or create one) that could be the same even after reboot the device. But it is important that the id be different even for 2 same devices. How can i do that?

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44
m n
  • 223
  • 1
  • 7
  • 19

5 Answers5

3

You can use device's IMEI number as unique Id.

You want to call android.telephony.TelephonyManager.getDeviceId().

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).

You'll need the following permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
Sahil Munjal
  • 463
  • 2
  • 15
1

You can generate different device token using GCM.... And this device token will remain same even if you will uninstall and again installed the application or after factory setting.. you have to follow some steps...... Create a new project at Google Developers Console . At this step, for simplicity, you just need to take note of 2 values: Project Number, which will be used as SENDER_ID in the client project; and API server key (created at Credentials), which will be used as API_KEY in the server project. Create a new simple Android project for server side (with basic source code as my answer in the following links).

Create a new simple Android project for client side (with basic source code as my answer in the following links, I customized from the original source at Google Cloud Messaging - GitHub).

Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app (or write code to send this token to server app). You can read more at the following questions, one of them you have read before with one of your previous questions:

How to implement a GCM Hello World for Android using Android Studio Adding Google Cloud Messagin (GCM) for Android - Registration process

S.Singh
  • 9
  • 3
1

Try this one String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);

Here android_id is the unique Id for each device.

Mr. Mad
  • 1,230
  • 1
  • 14
  • 28
1

Try this code

 String UniqueDeviceId = AndroidDeviceIdentifier.getUniqueDeviceIdentifier(context);

Add this class too.

final class AndroidDeviceIdentifier {

private AndroidDeviceIdentifier() {
    // hidden constructor of singleton
}

/**
 * Returns a stable identifier for the current device.
 *
 * @param ctx The application's Context
 * @return The unique device identifier
 * @throws IllegalStateException If the device's identifier could not be determined
 */
public static String getUniqueDeviceIdentifier(@NonNull final Context ctx) throws IllegalStateException {
    try {
        return getDeviceUUID(ctx);
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
        throw new IllegalStateException("Could not determine device identifier", e);
    }
}

private static String getDeviceUUID(Context ctx) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] hash = makeHash(getMac(ctx), getSerialNumber(ctx));
    return createUUIDFromHash(hash);
}

private static String createUUIDFromHash(byte[] hash) {
    return UUID.nameUUIDFromBytes(hash).toString().toLowerCase(); // Server side wants lower cased UUIDs
}

private static byte[] makeHash(final String mac, final String serialNumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    MessageDigest sha;

    sha = MessageDigest.getInstance("SHA-256");
    sha.reset();

    sha.update(mac.getBytes("UTF-8"));
    sha.update(serialNumber.getBytes("UTF-8"));

    return sha.digest();
}

private static String getSerialNumber(Context context) {
    String serialNumber = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    if (serialNumber == null) {
        serialNumber = "0000000000000000";
    }

    return serialNumber;
}

private static String getMac(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String mac = wifiManager.getConnectionInfo().getMacAddress();
    if (mac == null) {
        mac = "000000000000";
    }
    return mac;
}

You will get a unique device id. Ping me if u have any questions

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cecil Paul
  • 595
  • 6
  • 27
0

Try out this code below ,this device id is constant and even if you uninstall the app and reinstall this id remains constant and you can use it to retrieve the user data from database as well.

final String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
            Settings.Secure.ANDROID_ID);

Just paste this code in your main activity or any function and can store the ID generated in the shared Preference for later use.