Les annonces interstitielles sont des annonces en plein écran qui couvrent l'interface de l'application. Vous diffusez ces annonces à des moments de transition naturels dans votre application, par exemple entre deux activités ou pendant les pauses entre les niveaux de jeu.
Lorsque votre application affiche une annonce interstitielle, les utilisateurs peuvent appuyer dessus et continuer, ou la fermer et revenir à l'application.
Ce guide explique comment intégrer des annonces interstitielles dans une application Android.
Avant de commencer
Avant de continuer, procédez comme suit :
- Configurez GMA Next-Gen SDK.
- Utilisez l'ID du bloc d'annonces interstitielles de test
ca-app-pub-3940256099942544/1033173712.- Lorsque vous créez et testez votre application, assurez-vous d'utiliser des annonces tests plutôt que des annonces de production. Si vous n'utilisez pas l'ID du bloc d'annonces de test, Google peut suspendre votre compte.
- Avant de publier votre application, remplacez cet ID par l'ID de votre bloc d'annonces.
- Pour en savoir plus sur les annonces tests GMA Next-Gen SDK, consultez Activer les annonces tests.
Charger une annonce
L'exemple suivant charge une seule annonce :
Kotlin
// Load ads after you initialize MobileAds. InterstitialAd.load( AdRequest.Builder(adUnitId).build(), object : AdLoadCallback<InterstitialAd> { override fun onAdLoaded(ad: InterstitialAd) { // Interstitial ad loaded. interstitialAd = ad } override fun onAdFailedToLoad(adError: LoadAdError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: ${adError.message}") interstitialAd = null } }, )
Java
// Load ads after you initialize MobileAds. InterstitialAd.load( new AdRequest.Builder(adUnitId).build(), new AdLoadCallback<InterstitialAd>() { @Override public void onAdLoaded(@NonNull InterstitialAd ad) { // Interstitial ad loaded. interstitialAd = ad; } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: " + adError.getMessage()); interstitialAd = null; } });
Pour les tests, utilisez l'ID de bloc d'annonces de test suivant : ca-app-pub-3940256099942544/1033173712.
Diffuser l'annonce
Pour afficher une annonce interstitielle, appelez la méthodeshow.
L'exemple suivant affiche l'annonce :
Kotlin
private fun showAd(interstitialAd: InterstitialAd, activity: Activity) { // Show the ad. interstitialAd.show(activity) }
Java
private void showAd(InterstitialAd interstitialAd, Activity activity) { // Show the ad. interstitialAd.show(activity); }
Écouter les événements d'annonce
Avant de diffuser l'annonce, écoutez les événements de cycle de vie de l'annonce. L'exemple suivant enregistre des rappels pour les événements d'annonce :
Kotlin
// Listen for ad events. val ad = interstitialAd if (ad == null) { Log.e(TAG, "Interstitial ad is not ready yet.") return } ad.adEventCallback = object : InterstitialAdEventCallback { override fun onAdShowedFullScreenContent() { // Interstitial ad did show. } override fun onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: ${fullScreenContentError.message}") } override fun onAdImpression() { // Interstitial ad did record an impression. } override fun onAdClicked() { // Interstitial ad did record a click. } }
Java
// Listen for ad events. if (interstitialAd == null) { Log.e(TAG, "Interstitial ad is not ready yet."); return; } interstitialAd.setAdEventCallback( new InterstitialAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Interstitial ad did show. } @Override public void onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null; } @Override public void onAdFailedToShowFullScreenContent( @NonNull FullScreenContentError fullScreenContentError) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: " + fullScreenContentError.getMessage()); } @Override public void onAdImpression() { // Interstitial ad did record an impression. } @Override public void onAdClicked() { // Interstitial ad did record a click. } });