2

How to get unically Deviceid in Android? For smartphones and tablets. As I understood it may be not unically?

Will this code works? http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

Hi Mans
  • 65
  • 2
  • 9
  • 1
    A related question with a well-written answer: http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id/2853253#2853253 – jbenowitz Aug 22 '13 at 14:21
  • Quote: "Telephony-based ID won't be there on tablet devices, neh?" – Hi Mans Aug 22 '13 at 14:23
  • You can use the AndroidID, but if a phone is rooted, it will be null. In my experience, I've used a bunch of these at once. Checked to see if androidID was null, if so, went with telephony id or mac address. – jbenowitz Aug 22 '13 at 14:32
  • Please refer this question: http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id – Chintan Soni Aug 22 '13 at 14:32
  • @jbenowitz I use a rooted SGS2 for development, the adMob SDK did show me an ID in the LogCat, however I have no clue where it came from – Droidman Aug 22 '13 at 15:19
  • Well, try the AndroidID way. I could be wrong. I've also done what is detailed here http://stackoverflow.com/a/5626208/811282 Not sure if mocking this will help you. – jbenowitz Aug 22 '13 at 16:09

3 Answers3

2

This is an easy way of getting the device Id that will also work on tablets

public class DeviceHelper {

private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";

public synchronized static String getDeviceId(final Context context) {
    if (sID == null) {
        File installation = new File(context.getFilesDir(), INSTALLATION);
        try {
            if (!installation.exists()){
                writeInstallationFile(context,installation);
            }
            sID = readInstallationFile(installation);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return sID;
}

private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile f = new RandomAccessFile(installation, "r");
    byte[] bytes = new byte[(int) f.length()];
    f.readFully(bytes);
    f.close();
    return new String(bytes);
}

private static void writeInstallationFile(final Context context,File installation) throws IOException {
    FileOutputStream out = new FileOutputStream(installation);
    StringBuffer buf=new StringBuffer();
    buf.append(Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID));
    buf.append("-");
    buf.append(UUID.randomUUID().toString());
    out.write(buf.toString().getBytes());
    out.close();
}

}
  • CAUTION!!!! According to the documentations, The value of `ANDROID_ID` may change if a factory reset is performed on the device – Bob Jun 25 '14 at 11:35
0

Simplest code to get Unique ID of an android device: (This works on tablets also.)

    String deviceId = Secure.getString(MainActivity.this.getContentResolver(),Secure.ANDROID_ID);
    Log.d("Device ID",deviceId);
SKK
  • 5,261
  • 3
  • 27
  • 39
0

Another possibility:

final TelephonyManager tm = (TelephonyManager) c
        .getSystemService(Context.TELEPHONY_SERVICE);
Log.w("ID", tm.getDeviceId());
Droidman
  • 11,485
  • 17
  • 93
  • 141