This tutorial is based on the following blog post: Identifying App Installations | Android Developers Blog
The standard way to get a unique id is using PhoneId.GetDeviceId. However this method has several disadvantages:
- It requires the "android.permission.READ_PHONE_STATE" permission which is quite a sensitive permission.
- It doesn't work on devices without phone functionality.
- Apparently there is a bug in some devices which return the same id.
Starting from Android 2.3 there is a new field that returns a unique id and should work on all devices (without requiring any permission).
A general solution is to use this field for modern devices and to use a "fake" id for older devices. The fake id is randomly created when the application first runs.
Here is the code (requires the Reflection
Sub Activity_Create(FirstTime As Boolean)
Sub GetDeviceId As String
Dim r As Reflector
Dim Api As Int
Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
If Api < 9 Then
If File.Exists(File.DirInternal, "__id") Then
Return File.ReadString(File.DirInternal, "__id")
Dim id As Int
id = Rnd(0x10000000, 0x7FFFFFFF)
File.WriteString(File.DirInternal, "__id", id)
Return r.GetStaticField("android.os.Build", "SERIAL")