Skip to content Skip to sidebar Skip to footer

Unable To Get Android Advertising ID

I'm experimenting with getting the Android Advertising Id, but I can't find the proper way. In fact, I can't even get the Advertising Id Provider. isAdvertisingIdProviderAvailable

Solution 1:

If you want to get the advertising id for your published app, use the following approach:

Add this line to your gradle file;

com.google.android.gms:play-services-ads-identifier:16.0.0

And the code like this;

  private void determineAdvertisingInfo(Context context) {

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                // You should check this in case the user disabled it from settings
                if (!advertisingIdInfo.isLimitAdTrackingEnabled()) {
                    String id = advertisingIdInfo.getId();
                    // getUserAttributes(id);
                } else {
                    //If you stored the id you should remove it
                }
            } catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }
        }
    });
}

There is a note specifying this in the document as well: Note


Post a Comment for "Unable To Get Android Advertising ID"