Skip to content

Android Interstitial Ad Integration

Overview

Interstitial ads are full-screen ads displayed when switching between application interfaces, with the following characteristics:

  • High exposure rate
  • Strong visual impact
  • Suitable for display during application scene transitions
  • Higher click-through and conversion rates

Integration Steps

Refer to the InterstitialAdHelper in the demo example

1. Initialize Interstitial Ad

Initialize interstitial ads in Activity or Fragment:

kotlin
/**
 * Interstitial ad helper class
 * 
 * Responsible for interstitial ad loading, display, and lifecycle management
 * 
 * @param activity Context Activity
 * @param logger Log printing tool
 */
class InterstitialAdHelper(private val activity: Activity, private val logger: PrintLogger) {

    /**
     * Interstitial ad object
     * 
     * Used to manage interstitial ad loading, display, and destruction
     */
    private var interstitialAdObject: InterstitialAdObject? = null

    /**
     * Load interstitial ad
     * 
     * This method creates ad configuration, initializes interstitial ad object, sets ad listener
     * Finally calls load() method to start loading ads
     */
    fun load() {
        // Create ad configuration object, set placement ID
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.INTERSTITIAL_ID, // Interstitial ad placement ID
        )
        
        // Initialize interstitial ad object
        interstitialAdObject = InterstitialAdObject(activity, adConfig)
        
        // Set ad listener to monitor various ad events
        interstitialAdObject?.setAdObjectListener(object : InterstitialAdObjectListener {
            /**
             * Ad loaded successfully callback
             * 
             * @param placementId Ad placement ID
             */
            override fun onLoadSuccess(placementId: String) {
                // Ad loaded successfully, can be displayed now, recommended to check isReady before display
                logger.add("Interstitial: onLoadSuccess")
            }

            /**
             * Ad loading failed callback
             * 
             * @param error Error information
             * @param placementId Ad placement ID
             */
            override fun onLoadError(
                error: UjuException,
                placementId: String
            ) {
                // Record loading error information
                logger.add("Interstitial: onLoadError:${error.message}")
                DemoLogUtils.e("Interstitial:onLoadError:${error.message}")
            }

            /**
             * Ad displayed successfully callback
             */
            override fun onAdShow() {
                // Get ad information, record ecpm value
                val adInfo = interstitialAdObject?.getAdInfo()
                logger.add("Interstitial: onAdShow:ecpm:${adInfo?.ecpm}")
                DemoLogUtils.d("Interstitial: onAdShow:ecpm:${adInfo?.ecpm}")
            }

            /**
             * Ad display error callback
             * 
             * @param error Error information
             * @param placementId Ad placement ID
             */
            override fun onAdError(
                error: UjuException,
                placementId: String
            ) {
                // Record ad error information
                logger.add("Interstitial: onAdError:${error.message}")
                DemoLogUtils.e("Interstitial:onAdError:${error.message}")
            }

            /**
             * Ad playback completed callback
             */
            override fun onAdPlayComplete() {
                // Record ad playback completion event
                logger.add("Interstitial: onAdPlayComplete")
            }

            /**
             * Ad clicked callback
             */
            override fun onAdClicked() {
                // Record ad click event
                logger.add("Interstitial: onAdClicked")
            }

            /**
             * Ad landing page closed callback
             */
            override fun onLpClosed() {
                // Record landing page close event
                logger.add("Interstitial: onLpClosed")
            }

            /**
             * Ad closed callback
             */
            override fun onAdClosed() {
                // Destroy ad object, release resources
                interstitialAdObject?.destroy()
                interstitialAdObject = null
                // Record ad close event
                logger.add("Interstitial: onAdClosed")
            }
        })
        
        // Start loading ad
        interstitialAdObject?.load()
        // Record ad loading event
        logger.add("Interstitial: load, placementId:${adConfig.placementId}")
        DemoLogUtils.d("Interstitial: load, placementId:${adConfig.placementId}")
    }

    /**
     * Display interstitial ad
     * 
     * Before displaying the ad, it checks if the ad is ready
     * Only when the ad status is ready, it calls show() method to display the ad
     */
    fun show() {
        // Check if ad is ready
        if (interstitialAdObject?.isReady() == true) {
            // Ad is ready, display ad
            interstitialAdObject?.show(activity)
        } else {
            // Ad is not ready, record log
            logger.add("Interstitial: Ad not ready yet")
        }
    }

    /**
     * Check if ad is loaded
     * 
     * @return true if ad object is created, false otherwise
     * Note: This method only checks if ad object exists, does not guarantee ad is ready
     * To check if ad can be displayed, use isReady() method
     */
    fun isLoaded(): Boolean {
        return interstitialAdObject != null
    }

    /**
     * Destroy ad object
     * 
     * When ad is no longer needed, call this method to destroy ad object and release resources
     */
    fun destroy() {
        // Destroy ad object
        interstitialAdObject?.destroy()
        // Set ad object reference to null
        interstitialAdObject = null
    }
}

2. Display Interstitial Ad

Display interstitial ads at appropriate times, such as when game levels are completed, app starts, page switches, etc:

kotlin
/**
 * Display interstitial ad
 * 
 * Check if ad is loaded, display if loaded, otherwise reload
 */
private fun showInterstitialAd() {
    if (interstitialAdObject != null && interstitialAdObject?.isReady() == true) {
        // Ad is loaded, display ad
        interstitialAdObject?.show(activity)
    } else {
        // Ad not loaded, prompt user or reload
        Log.d("InterstitialAd", "Ad not loaded, reloading")
        interstitialAdObject?.load()
    }
}

3. Ad Display Timing

Interstitial ads are suitable for display in the following scenarios:

kotlin
// Scenario 1: Game level completed
private fun onLevelComplete() {
    // Level completion logic
    // ...
    
    // Display interstitial ad
    showInterstitialAd()
}

// Scenario 2: After app startup
private fun onAppStarted() {
    // App startup logic
    // ...
    
    // Delay displaying interstitial ad to let user see app content first
    Handler(Looper.getMainLooper()).postDelayed({ showInterstitialAd() }, 3000)
}

// Scenario 3: Page switching
private fun onPageChanged() {
    // Page switching logic
    // ...
    
    // Display interstitial ad
    showInterstitialAd()
}

Best Practices

1. Ad Display Strategy

  • Reasonable display frequency: Avoid overly frequent display that affects user experience
  • Appropriate display timing: Display during natural user operation gaps
  • Preload strategy: Load ads in advance to ensure immediate display when needed
  • Frequency control: Set time intervals or frequency limits for ad display

2. Optimization Suggestions

  • Preload ads: Start loading ads when the app starts
  • Ad caching: Implement ad caching mechanism to improve display speed
  • Failure handling: Provide reasonable alternative solutions when ad loading fails
  • User experience: Ensure ads do not affect core app functions

3. Avoidable Issues

  • Don't force ads to be displayed during user's critical operations
  • Don't display ads immediately at app startup (recommended to delay a few seconds)
  • Don't set too short ad display intervals
  • Don't display too many ads in the same user session

Common Questions

Q: Why isn't the interstitial ad displaying?

A: Possible reasons:

  • Incorrect ad unit ID
  • Network connection issues
  • Insufficient ad inventory
  • Ad not loaded completely
  • Device restrictions

Q: How to improve interstitial ad effectiveness?

A: Recommendations:

  • Choose appropriate ad display timing
  • Optimize ad loading and display strategies
  • Ensure ads are relevant to app content
  • Increase app user activity
  • Analyze ad performance data and continuously optimize

Q: Can interstitial ads be loaded in background threads?

A: Yes, but need to pay attention to:

  • Ad creation and loading need to be done in the main thread
  • Can use Handler or AsyncTask to manage loading timing
  • Ensure ads are displayed in the UI thread