MobileAds.Companion

public static class MobileAds.Companion


Summary

Public methods

static final @NonNull SignalGenerationResult

Generates a signal that can be used as input in a server-to-server ad request.

static final void

Generates a signal that can be used as input in a server-to-server ad request.

static final @NonNull InitializationStatus

Gets the SDK initialization status.

static final @NonNull RequestConfiguration

Gets the global RequestConfiguration.

static final @NonNull VersionInfo

Gets the external version (for example, 20.6.0) of the Google Mobile Ads SDK.

static final void
@WorkerThread
@RequiresPermission(value = "android.permission.INTERNET")
initialize(
    @NonNull Context context,
    @NonNull InitializationConfig initializationConfig,
    OnAdapterInitializationCompleteListener listener
)

Initializes the Google Mobile Ads SDK synchronously.

static final boolean

Indicates whether the Google Mobile Ads SDK is ready to handle method calls other than initialize.

static final void

Opens the ad inspector UI.

static final void
openDebugMenu(@NonNull Activity activity, @NonNull String adUnitId)

Opens the debug menu.

static final boolean

Controls whether the Google Mobile Ads SDK publisher first-party ID, formerly known as the same app key, is enabled.

static final CustomTabsSession
registerCustomTabsSession(
    @NonNull CustomTabsClient client,
    @NonNull String origin,
    CustomTabsCallback callback
)

Registers a CustomTabsClient with the Google Mobile Ads SDK to improve in-app ad monetization of ads within this CustomTabsSession.

static final void

Registers a WebView with the Google Mobile Ads SDK to improve in-app ad monetization of ads within this WebView.

static final void
setRequestConfiguration(
    @NonNull RequestConfiguration requestConfiguration
)

Sets the global RequestConfiguration that will be used for every AdRequest during the app's session.

static final void

Sets the user-controlled application's audio volume.

static final void
setUserMutedApp(boolean muted)

Indicates whether the user muted or unmuted the app.

Public methods

generateSignal

public static final @NonNull SignalGenerationResult generateSignal(@NonNull SignalRequest request)

Generates a signal that can be used as input in a server-to-server ad request. Suspends until a SignalGenerationResult is returned.

Parameters
@NonNull SignalRequest request

A SignalRequest with targeting information.

Returns
@NonNull SignalGenerationResult

SignalGenerationResult.Success if signals are successfully generated, otherwise SignalGenerationResult.Failure.

generateSignal

public static final void generateSignal(
    @NonNull SignalRequest request,
    @NonNull SignalGenerationCallback callback
)

Generates a signal that can be used as input in a server-to-server ad request.

Parameters
@NonNull SignalRequest request

A signal request with targeting information.

@NonNull SignalGenerationCallback callback

A callback to be invoked when signal generation completes.

getInitializationStatus

public static final @NonNull InitializationStatus getInitializationStatus()

Gets the SDK initialization status.

getRequestConfiguration

public static final @NonNull RequestConfiguration getRequestConfiguration()

Gets the global RequestConfiguration.

getVersion

public static final @NonNull VersionInfo getVersion()

Gets the external version (for example, 20.6.0) of the Google Mobile Ads SDK.

initialize doesn't need to be called prior to calling this function.

initialize

@WorkerThread
@RequiresPermission(value = "android.permission.INTERNET")
public static final void initialize(
    @NonNull Context context,
    @NonNull InitializationConfig initializationConfig,
    OnAdapterInitializationCompleteListener listener
)

Initializes the Google Mobile Ads SDK synchronously.

When this method returns, the SDK is ready to accept requests but not all 3P SDKs may be ready yet. listener will be invoked when adapter initialization has finished.

Call this method as early as possible after the app launches to reduce latency on the session's first ad request, but balance doing so with the startup time for your users.

This method must be called prior to interacting with MobileAds, unless explicitly noted (see getVersion). Otherwise, an UninitializedPropertyAccessException may be thrown. Calling initialize will make isInitialized return true.

This method should be called on a background thread, as it may perform disk or network operations. Failure to do so may cause an "application not responding" (ANR) dialog.

Multiple calls before initialization has finished will queue up listeners. Calls after initialization has finished will immediately invoke listener. Calling initialize again with a different application ID is a no-op.

Parameters
@NonNull Context context

The Context the SDK is running in.

@NonNull InitializationConfig initializationConfig

Options to control SDK initialization.

OnAdapterInitializationCompleteListener listener

A callback to be invoked upon initialization completion.

isInitialized

public static final boolean isInitialized()

Indicates whether the Google Mobile Ads SDK is ready to handle method calls other than initialize. This will return true after initialize has been called.

It is important to wait until this property is true prior to interacting with other methods on MobileAds.

Attempting to do so before this property is true may result in an UninitializedPropertyAccessException, unless explicitly noted (see getVersion).

openAdInspector

public static final void openAdInspector(@NonNull OnAdInspectorClosedListener listener)

Opens the ad inspector UI.

openDebugMenu

public static final void openDebugMenu(@NonNull Activity activity, @NonNull String adUnitId)

Opens the debug menu.

Parameters
@NonNull Activity activity

The Activity the SDK is running in.

@NonNull String adUnitId

Any Ad Manager ad unit ID associated with your app.

putPublisherFirstPartyIdEnabled

public static final boolean putPublisherFirstPartyIdEnabled(boolean enabled)

Controls whether the Google Mobile Ads SDK publisher first-party ID, formerly known as the same app key, is enabled.

The ID is enabled by default. When disabled, the setting is persisted across app sessions and the ID is cleared.

Parameters
boolean enabled

Boolean true if the publisher first-party ID is enabled, false otherwise.

Returns
boolean

true if the enablement decision has been successfully persisted. False if the decision fails to be persisted.

registerCustomTabsSession

public static final CustomTabsSession registerCustomTabsSession(
    @NonNull CustomTabsClient client,
    @NonNull String origin,
    CustomTabsCallback callback
)

Registers a CustomTabsClient with the Google Mobile Ads SDK to improve in-app ad monetization of ads within this CustomTabsSession. This will only create one instance of CustomTabsSession and re-running this overrides the existing session.

Parameters
@NonNull CustomTabsClient client

CustomTabsClient used to create the CustomTabsSession instance.

@NonNull String origin

String used as origin on the Digital Asset Link for verification.

CustomTabsCallback callback

optional CustomTabsCallback to receive callbacks from CustomTabsSession.

Returns
CustomTabsSession

CustomTabsSession instance to be used for Custom Tabs Intent when opening a Custom Tabs page. Null if the client failed to return a new session.

registerWebView

public static final void registerWebView(@NonNull WebView webView)

Registers a WebView with the Google Mobile Ads SDK to improve in-app ad monetization of ads within this WebView.

setRequestConfiguration

public static final void setRequestConfiguration(
    @NonNull RequestConfiguration requestConfiguration
)

Sets the global RequestConfiguration that will be used for every AdRequest during the app's session.

setUserControlledAppVolume

public static final void setUserControlledAppVolume(float volume)

Sets the user-controlled application's audio volume. Affects audio volumes of all ads relative to other audio output.

Warning: Lowering your app's audio volume reduces video ad eligibility and may reduce your app's ad revenue. You should only utilize this API if your app provides custom volume controls to the user, and you should reflect the user's volume choice in this API.

Parameters
float volume

The volume as a float from 0 (muted) to 1.0 (full media volume). Defaults to 1.0

Throws
java.lang.IllegalArgumentException

if volume is out of the valid range

setUserMutedApp

public static final void setUserMutedApp(boolean muted)

Indicates whether the user muted or unmuted the app. Affects initial mute state for all ads.

Warning: Muting your application reduces video ad eligibility and may reduce your app's ad revenue. You should only utilize this API if your app provides a custom mute control to the user, and you should reflect the user's mute decision in this API.

Parameters
boolean muted

True if the app is muted, false otherwise. Defaults to false.