Class AdManager

java.lang.Object
com.codename1.ads.AdManager

public final class AdManager extends Object

The entry point of the Codename One advertising API. It holds the active AdProvider and exposes the lifecycle integrations (interstitial-on-transition and app open ads) that build on the Codename One form and application lifecycle.

A provider is supplied by an ad library and registered once at startup. Each library exposes a static install() method that registers its provider, so wiring AdMob is a single line:

public void init(Object context) {
    AdMobProvider.install();
    AdManager.initialize(new AdConfig(), ready -> AdConsent.requestConsent(null));
}

Any object implementing AdProvider can be registered with registerProvider(AdProvider) directly, so third party ad networks and mediation layers plug in without changing the framework or the build.

When no provider is registered the API degrades gracefully: format support reports false and loads fail with AdError.CODE_UNSUPPORTED instead of throwing.

  • Method Details

    • registerProvider

      public static void registerProvider(AdProvider p)

      Registers the ad provider to use, replacing any previously registered provider. Normally called by a provider's static install() method.

      Parameters
      • p: the provider to use
    • getProvider

      public static AdProvider getProvider()
      The active ad provider, or null when none is registered or the registered provider is unsupported on this platform.
    • getConfig

      public static AdConfig getConfig()
      The configuration passed to initialize(AdConfig, AdCallback), or null if it has not been called.
    • isSupported

      public static boolean isSupported(AdFormat format)

      True when the active provider supports the given format.

      Parameters
      • format: the format to query
    • initialize

      public static void initialize(AdConfig cfg, AdCallback<Boolean> onComplete)

      Initializes the active provider's SDK. The callback fires when initialization completes (immediately with Boolean.FALSE when no provider is registered).

      Parameters
      • cfg: the global ad configuration, must not be null
      • onComplete: invoked with the readiness flag, may be null
    • isInitialized

      public static boolean isInitialized()
      True once initialize(AdConfig, AdCallback) has completed successfully.
    • bindInterstitialOnTransition

      public static void bindInterstitialOnTransition(InterstitialAd ad, int minIntervalMillis)

      Shows the supplied interstitial automatically on screen transitions, no more often than minIntervalMillis.

      The manager loads the first ad, shows it when a transition occurs and the interval has elapsed, and preloads the next one when the ad is dismissed. Any AdListener you set on the ad is still notified.

      Parameters
      • ad: the interstitial to show on transitions
      • minIntervalMillis: minimum time between two shown ads
    • enableAppOpenAds

      public static void enableAppOpenAds(AppOpenAd ad)

      Loads the supplied app open ad and lets the provider show it automatically whenever the application returns to the foreground.

      Parameters
      • ad: the app open ad to manage