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
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
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();
}
}
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);
Another possibility:
final TelephonyManager tm = (TelephonyManager) c
.getSystemService(Context.TELEPHONY_SERVICE);
Log.w("ID", tm.getDeviceId());