מודעות מעברונים

מודעות מעברון הן מודעות במסך מלא שמופיעות מעל הממשק של האפליקציה המארחת. בדרך כלל הן מוצגות בנקודות מעבר טבעיות בזרימה של האפליקציה, למשל בין פעילויות או במהלך ההשהיה בין שלבים במשחק. כשאפליקציה מציגה מודעת מעברון, המשתמש יכול ללחוץ על המודעה ולהמשיך ליעד שלה, או לסגור אותה ולחזור לאפליקציה.

במדריך הזה מוסבר איך לשלב מודעות מעברון באפליקציה ל-Android.

דרישות מוקדמות

טעינת מודעה

כדי לטעון מודעת מעברון, צריך להפעיל את השיטה הסטטית AdManagerInterstitialAd של load() ולהעביר את AdManagerInterstitialAdLoadCallback כדי לקבל את המודעה שנטענה או שגיאות אפשריות. ‫AdManagerInterstitialAdLoadCallback משתמש ב-LoadAdError כדי לספק פרטים על השגיאה.

Java

AdManagerInterstitialAd.load(
    this,
    AD_UNIT_ID,
    new AdManagerAdRequest.Builder().build(),
    new AdManagerInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
        Log.d(TAG, "Ad was loaded.");
        MyActivity.this.interstitialAd = interstitialAd;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        Log.d(TAG, loadAdError.getMessage());
        interstitialAd = null;
      }
    });

Kotlin

AdManagerInterstitialAd.load(
  this,
  AD_UNIT_ID,
  AdManagerAdRequest.Builder().build(),
  object : AdManagerInterstitialAdLoadCallback() {
    override fun onAdLoaded(interstitialAd: AdManagerInterstitialAd) {
      Log.d(TAG, "Ad was loaded.")
      this@MyActivity.interstitialAd = interstitialAd
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, adError.message)
      interstitialAd = null
    }
  },
)

הגדרת FullScreenContentCallback

ה-FullScreenContentCallback מטפל באירועים שקשורים להצגת ה-InterstitialAd. לפני שמציגים את InterstitialAd, חשוב להגדיר את הקריאה החוזרת (callback):

Java

interstitialAd.setFullScreenContentCallback(
    new FullScreenContentCallback() {
      @Override
      public void onAdDismissedFullScreenContent() {
        // Called when fullscreen content is dismissed.
        Log.d(TAG, "The ad was dismissed.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        MyActivity.this.interstitialAd = null;
      }

      @Override
      public void onAdFailedToShowFullScreenContent(AdError adError) {
        // Called when fullscreen content failed to show.
        Log.d(TAG, "The ad failed to show.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        MyActivity.this.interstitialAd = null;
      }

      @Override
      public void onAdShowedFullScreenContent() {
        // Called when fullscreen content is shown.
        Log.d(TAG, "The ad was shown.");
      }

      @Override
      public void onAdImpression() {
        // Called when an impression is recorded for an ad.
        Log.d(TAG, "The ad recorded an impression.");
      }

      @Override
      public void onAdClicked() {
        // Called when ad is clicked.
        Log.d(TAG, "The ad was clicked.");
      }
    });

Kotlin

interstitialAd?.fullScreenContentCallback =
  object : FullScreenContentCallback() {
    override fun onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d(TAG, "Ad was dismissed.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      interstitialAd = null
    }

    override fun onAdFailedToShowFullScreenContent(adError: AdError) {
      // Called when fullscreen content failed to show.
      Log.d(TAG, "Ad failed to show.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      interstitialAd = null
    }

    override fun onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      Log.d(TAG, "Ad showed fullscreen content.")
    }

    override fun onAdImpression() {
      // Called when an impression is recorded for an ad.
      Log.d(TAG, "Ad recorded an impression.")
    }

    override fun onAdClicked() {
      // Called when ad is clicked.
      Log.d(TAG, "Ad was clicked.")
    }
  }

הצגת המודעה

צריך להציג מודעות מעברון בנקודות עצירה טבעיות בזרימה של האפליקציה. למשל, בין שלבים במשחק או אחרי שהמשתמש משלים משימה. כדי להציג מודעת מעברון, משתמשים ב-method‏ show.

Java

if (interstitialAd != null) {
  interstitialAd.show(this);
} else {
  Log.d(TAG, "The interstitial ad is still loading.");
}

Kotlin

interstitialAd?.show(this)

תמיד צריך לבדוק באמצעות מודעות בדיקה

כשאתם מפתחים ובודקים את האפליקציות, חשוב להשתמש במודעות בדיקה ולא במודעות פעילות. אחרת, אנחנו עלולים להשעות את החשבון.

הדרך הכי קלה לטעון מודעות בדיקה היא להשתמש במזהה הייעודי של יחידת המודעות לבדיקה של מודעות מעברון ב-Android:

/21775744923/example/interstitial

הוא הוגדר במיוחד כדי להחזיר מודעות בדיקה לכל בקשה, ואתם יכולים להשתמש בו באפליקציות שלכם בזמן התכנות, הבדיקה וניפוי הבאגים. רק חשוב להחליף אותו במזהה יחידת המודעות שלכם לפני שמפרסמים את האפליקציה.

פרטים על מודעות בדיקה ב-Google Mobile Ads SDK (Legacy) זמינים במאמר הפעלת מודעות בדיקה.

דוגמאות ב-GitHub

  • דוגמה למודעות מעברון: Java | Kotlin

השלבים הבאים