I want to get users Advertising ID programmatically.I used the below code from the developer site.But its not working
Info adInfo = null;
Fetch the advertising id from background thread:
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
String adId = adInfo != null ? adInfo.getId() : null;
// Use the advertising id
} catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) {
// Error handling if needed
}
}
});
I added null
checks to prevent any crashes. The Google example implementation code would crash with a NullPointerException
if an exception occures.