טעינה של מודעת מעברון יחידה

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

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

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

לפני שמתחילים

לפני שממשיכים, צריך לבצע את הפעולות הבאות:

  • מגדירים את GMA Next-Gen SDK.
  • משתמשים במזהה של יחידת מודעות המעברון לבדיקה ca-app-pub-3940256099942544/1033173712.
    • כשאתם מפתחים ובודקים את האפליקציה, חשוב להשתמש במודעות בדיקה ולא במודעות פעילות בשלב ההפקה. אם לא תשתמשו במזהה יחידת המודעות הייעודית לבדיקה, Google עלולה להשעות את החשבון שלכם.
    • לפני שמפרסמים את האפליקציה, צריך להחליף את המזהה הזה במזהה של יחידת המודעות.
    • פרטים על מודעות בדיקה ב-GMA Next-Gen SDK זמינים במאמר הפעלת מודעות בדיקה.

טעינת מודעה

בדוגמה הבאה נטענת מודעה אחת:

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;
      }
    });

בזמן הבדיקה, צריך להשתמש במזהה יחידת המודעות הייעודית לבדיקה: ca-app-pub-3940256099942544/1033173712.

הצגת המודעה

כדי להציג מודעת מעברון, צריך להפעיל את השיטה show.

בדוגמה הבאה מוצגת המודעה:

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);
}

האזנה לאירועי מודעות

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

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.
      }
    });