AI Integration Agent Instructions for Interstitial Ads
Required Imports
Use the following imports to implement an interstitial ad:
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAd
import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAdEventCallbackMethod Signatures
Use the following method signatures to implement interstitial ads:
The AdRequest object takes the ad unit ID:
AdRequest.Builder(adUnitId: String).build()The method signature to load an ad from InterstitialAd:
fun load(
adRequest: AdRequest,
adLoadCallback: AdLoadCallback<InterstitialAd>
): UnitThe AdLoadCallback interface is defined as:
interface AdLoadCallback<T> {
fun onAdLoaded(ad: T): Unit
fun onAdFailedToLoad(adError: LoadAdError): Unit
}Gotchas
UI Threading: MANDATORY: Callbacks in GMA-Next Gen SDK are invoked on a
background thread. ALL UI-RELATED OPERATIONS (e.g., Toasts, View updates,
Fragment transactions) MUST be wrapped in runOnUiThread {} or
Dispatchers.Main.launch {} within GMA SDK callbacks. SKIPPING THIS STEP WILL
CAUSE THE APPLICATION TO CRASH.
Interstitial ad workflow
Load the ad
Register for ad event callbacks
- Set the
adEventCallbackon theInterstitialAdobject.- Drop the reference to the interstitial ad when the ad is dismissed or fails to show.
- Set the
Show the ad
Verify the implementation: Verify the build to ensure there are no compile errors:
- Run
gradle build -x testto confirm a successful clean build. Resolve any GMA SDK related compile errors.
- Run
Links
Additional documentation: