Tuesday, February 20, 2018

android - Device ID Giving null Value




I am trying to get Unique ID[I.E unique id of a device] by using below code



TelephonyManager tManager;


tManager = (TelephonyManager).getSystemService(Context.TELEPHONY_SERVICE);

String deviceId = tManager.getDeviceId();


Where deviceId Gives me Unique ID for Android Device. I am not testing this on emulator because in emulator i am getting value 000000000000000



Any way i am testing it on real devices,and this works fine for mostly all devices , but in some device i am getting value null



I have tested this on




Samsung Galaxy S3

Samsung Galaxy Tab3

Samsung Galaxy Star

Google Nexus4

Google Nexus5


Google Nexus7


In all the devices listed above it gives me correct out put except one Device and that is Google Nexus7, in this i m getting value null



So, ultimately my goal is to get unique value for each particular device, In case if this code gives me null value than i can try some alternate way



i heard that Device MAC Address is a unique for all devices, But unfortunately it just works on wifi connection.




So is there any other way to get unique value of each particular device?



Thanks Any way


Answer



You shouldn't really use the ANDROID_ID, you have no real guarantee that it will give you a unique identifier for the device and it is known that some older devices give the value null, or as in Moto's case, the same static value across multiple devices. This value also changes when the phone is reset to the factory default, and is super easy to change on a rooted device.



The correct way is to generate your own UUID via.



String uudid = UUID.randomUUID()



You then save this in your App, for example through a SharedPreference. You can then use Google's Android backup storage to retain this identifier even if the user uninstalls your App.



More information on Backup here: http://developer.android.com/guide/topics/data/backup.html



If you don't take my word for it, you should take Reto Meier's word for it, he is a developer advocate at the Google Android team. This video will explain what I just told you, check out around minute 15, he also specifically instructs developers not to use the ANDROID_ID.



http://www.youtube.com/watch?v=twmuBbC_oB8&list=WL86437554BC3E54B5


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...