سؤال

i am using following code in android to get IMEI number of phone

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId().toString();

i have tested it on many phones

1) Samsung Ace (froyo) 2) Galaxy S 3) Galaxy S2 4) Samsung Geo (ginger bread)

works perfect and gives IMEI number

but while running same code on following deivce i get nothing :(

Android Pantech

any one guide me what could be the issue or if i use Device_ID to uniquely identify android device would that work for all devices?

and one more thing i have read in forum it is some times null and few famous devices give same Device_ID

any one put some light on it that what is the best way to uniquely identify device and that piece of code should work on all devices?

any help would be appreciated.

هل كانت مفيدة؟

المحلول

any one put some light on it that what is the best way to uniquely identify device and that piece of code should work on all devices?

Unfortunatelly there is no 100% perfect way to identify an Android device. This is because Google does not provide a reliable way for this. Instead it recommends to track application installations (versus tracking devices). Here is a nice talk on this: Identifying App Installations.

نصائح أخرى

devices that do not have sim cards do not have IMEI numbers. you can read WIFI-MAC address instead (assuming that every android device will have WIFI)

I have created a logic to get unique value of all the device in my idea. Please have a look at this and correct me if it gets failed. And I tested on 10-20 devices and tried to install same rom I got unique value and the value is same after flashing the new rom too. Below is the method to get a unique value:

public static String getDeviceId(Context context) {

    final TelephonyManager tm = (TelephonyManager)     context.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = ""
            + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    String deviceId = deviceUuid.toString();
    return deviceId;

/*Below line fails on android 4.4 devices sometime so i made the above lines*/
 // return Secure.getString(activity.getContentResolver(), Secure.ANDROID_ID);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top