Android ID Generate getAndroidId(Context context)

Here you can find the source of getAndroidId(Context context)

Description

get Android Id

License

Open Source License

Declaration

public static String getAndroidId(Context context) 

Method Source Code

//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.content.Context;

import android.util.Log;

public class Main {
    private static final String HEX_DIGITS = "0123456789abcdef";
    private static final String LOGTAG = "Plus1Helper";

    public static String getAndroidId(Context context) {
        String androidId = android.provider.Settings.Secure.getString(
                context.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);

        if (androidId != null)
            return getHash(androidId);

        return null;
    }/* w  ww  .  jav  a  2  s.  c o  m*/

    public static String getHash(String text) {
        try {
            return SHA1(text);
        } catch (NoSuchAlgorithmException e) {
            Log.e(LOGTAG, "NoSuchAlgorithmException: " + e.toString(), e);

            return null; // FIXME: add other hash logic
        }
    }

    private static String SHA1(String text) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        byte[] sha1hash = new byte[40];
        md.update(text.getBytes());
        sha1hash = md.digest();

        return convertToHex(sha1hash);
    }

    private static String convertToHex(byte[] raw) {
        final StringBuilder hex = new StringBuilder(raw.length * 2);

        for (final byte b : raw) {
            hex.append(HEX_DIGITS.charAt((b & 0xF0) >> 4)).append(
                    HEX_DIGITS.charAt((b & 0x0F)));
        }

        return hex.toString();
    }
}

Related

  1. generateSyncEventId()
  2. getSequenceId()
  3. generateId(Object... value)
  4. generateId(Object... value)
  5. generateRecordingIdentifier()
  6. generateKey()
  7. generateRandomUIID()