As Giovanni says, change your code to:
String deviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), InstanceID.getInstance(getApplicationContext()).getId());
or
String deviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()).getId());
如果 在 中国 大陆 发布 此 app, 请勿 使用 以上 方法, 可能 导致 NullPointException, 因为 大部分 设备 都 没有 谷 歌 Service, 无法 获取 此 id. 请 使用
android.provider.Settings.Secure.ANDROID_ID
然后 加上 lint 屏蔽
@SuppressLint("HardwareIds")
In English: if your application is to be released in China, the above method may result in a NullPointException.Because most devices in China do not have a Google service. Try as follows:
android.provider.Settings.Secure.ANDROID_ID
and add suppressLint:
@SuppressLint("HardwareIds")
source share