AdvertisingIdClient

public class AdvertisingIdClient


Client for retrieving Advertising ID related info from an AndroidX ID Provider installed on the device.

Typical usage would be:

  1. Call isAdvertisingIdProviderAvailable to make sure there is an Advertising ID Provider available.
  2. Call getAdvertisingIdInfo to get Advertising ID info (the Advertising ID and LAT setting).

Summary

Public methods

static @NonNull ListenableFuture<AdvertisingIdInfo>

Retrieves the user's Advertising ID info.

static boolean

Checks whether there is any Advertising ID Provider installed on the device.

Public methods

getAdvertisingIdInfo

public static @NonNull ListenableFuture<AdvertisingIdInfogetAdvertisingIdInfo(@NonNull Context context)

Retrieves the user's Advertising ID info.

When multiple Advertising ID Providers are installed on the device, this method will always return the Advertising ID information from same Advertising ID Provider for all apps which use this library, using following priority:

  1. System-level providers with "androidx.ads.identifier.provider.HIGH_PRIORITY" permission
  2. Other system-level providers

If there are ties in any of the above categories, it will use this priority:

  1. First app by earliest install time (firstInstallTime)
  2. First app by package name alphabetically sorted
Parameters
@NonNull Context context

Current Context (such as the current android.app.Activity).

Returns
@NonNull ListenableFuture<AdvertisingIdInfo>

A ListenableFuture that will be fulfilled with a AdvertisingIdInfo which contains the user's Advertising ID info, or rejected with the following exceptions,

  • IOException signaling connection to Advertising ID Providers failed.
  • AdvertisingIdNotAvailableException indicating Advertising ID is not available, like no Advertising ID Provider found or provider does not return an Advertising ID.
  • TimeoutException indicating timeout period (20s) has expired.
  • InterruptedException indicating the current thread has been interrupted.

isAdvertisingIdProviderAvailable

public static boolean isAdvertisingIdProviderAvailable(@NonNull Context context)

Checks whether there is any Advertising ID Provider installed on the device.

This method does a quick check for the Advertising ID providers.

Note: Even if this method returns true, there is still a possibility that the getAdvertisingIdInfo method throws an exception for some reason.

Parameters
@NonNull Context context

Current Context (such as the current android.app.Activity).

Returns
boolean

whether there is an Advertising ID Provider available on the device.