What is the best way to get unique ID in Android? -


from thread, can settings.secure#android_id can null sometimes. on other hand, telephony-based id can null in tablet devices , can changed if user change sim card or flight mode.

thinking of getting mac address, thread, mac address cannot got too.

is there solution can android unique id won't change in condition?

rendy, code used above (from emmby answer (with minimal modifications) of question is there unique android device id?):

public class deviceuuidfactory {     protected static final string prefs_file = "device_id.xml";     protected static final string prefs_device_id = "device_id";     protected volatile static uuid uuid;      public deviceuuidfactory(context context) {         if (uuid == null) {             synchronized (deviceuuidfactory.class) {                 if (uuid == null) {                     final sharedpreferences prefs = context.getsharedpreferences(prefs_file, 0);                     final string id = prefs.getstring(prefs_device_id, null);                     if (id != null) {                         // use ids computed , stored in                         // prefs file                         uuid = uuid.fromstring(id);                     } else {                         final string androidid = secure.getstring(context.getcontentresolver(), secure.android_id);                         // use android id unless it's broken, in case                         // fallback on deviceid,                         // unless it's not available, fallback on random                         // number store                         // prefs file                         try {                             if (!"9774d56d682e549c".equals(androidid)) {                                 uuid = uuid.nameuuidfrombytes(androidid.getbytes("utf8"));                             } else {                                 final string deviceid = ((telephonymanager) context.getsystemservice(context.telephony_service)).getdeviceid();                                 uuid = (deviceid != null ? uuid.nameuuidfrombytes(deviceid.getbytes("utf8")) : uuid.randomuuid());                             }                         } catch (unsupportedencodingexception e) {                             throw new runtimeexception(e);                         }                          // write value out prefs file                         prefs.edit().putstring(prefs_device_id, uuid.tostring()).commit();                     }                 }             }         }     }      /**      * returns unique uuid current android device. uuids,      * unique id "very highly likely" unique across android      * devices. more android_id is.      *       * uuid generated using android_id base key if appropriate,      * falling on telephonymanager.getdeviceid() if android_id known      * incorrect, , falling on random uuid that's persisted      * sharedpreferences if getdeviceid() not return usable value.      *       * in rare circumstances, id may change. in particular, if      * device factory reset new device id may generated. in addition, if      * user upgrades phone buggy implementations of android      * 2.2 newer, non-buggy version of android, device id may change.      * or, if user uninstalls app on device has neither proper      * android id nor device id, id may change on reinstallation.      *       * note if code falls on using telephonymanager.getdeviceid(),      * resulting id not change after factory reset.      * aware of.      *       * works around bug in android 2.2 many devices when using android_id      * directly.      *       * @see http://code.google.com/p/android/issues/detail?id=10603      *       * @return uuid may used uniquely identify device      *         purposes.      */     public uuid getdeviceuuid() {         return uuid;     } } 

to use in activity, following:

uuid identifier = new deviceuuidfactory(this).getdeviceuuid(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -