Dieses Dokument wurde aktualisiert.
Die Übersetzung ins Deutsche ist noch nicht fertig.
Englisch aktualisiert: 18. Nov.
Deutsch aktualisiert: 23.01.2023

Add Interstitial Ads to an Android App

The Audience Network allows you to monetize your Android apps with Facebook ads. An interstitial ad is a full screen ad that you can show in your app. Typically interstitial ads are shown when there is a transition in your app. For example -- after finishing a level in a game or after loading a story in a news app.

Stelle sicher, dass du die Leitfäden zu den ersten Schritten mit dem Audience Network und mit Android abgeschlossen hast, bevor du fortfährst.

Steps-by-Step

Step 1: Initializing Interstitial Ads in your Activity

Step 2: Showing Interstitial Ads in your Activity

Initialisiere das Audience Network-SDK

Diese Methode wurde in Android Audience Network SDK Version 5.1 hinzugefügt

Seit Version 5.3.0 ist die Initialisierung des Audience Network-SDK für Android obligatorisch In diesem Dokument finden Sie Informationen zur Initialisierung des Audience Network-SDK für Android.

Vor der Erstellung eines Werbeanzeigenobjekts und dem Laden von Werbeanzeigen müssen Sie Audience Network-SDK initialisieren. Dies wird beim App-Launch empfohlen.

public class YourApplication extends Application {
    ...
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Audience Network SDK
        AudienceNetworkAds.initialize(this);       
    }
    ...
}

Step 1: Initializing Interstitial Ads in your Activity

Add the following code at the top of your Activity in order to import the Facebook Ads SDK:

import com.facebook.ads.*;

Initialize the InterstitialAd.

private InterstitialAd interstitialAd;

@Override
public void onCreate(Bundle savedInstanceState) {
...
  // Instantiate an InterstitialAd object. 
  // NOTE: the placement ID will eventually identify this as your App, you can ignore it for
  // now, while you are testing and replace it later when you have signed up.
  // While you are using this temporary code you will only get test ads and if you release
  // your code like this to the Google Play your users will not receive ads (you will get a no fill error).
  interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
...  

Step 2: Showing Interstitial Ads

Scenario 1: Create an InterstitialAdListener, load the Ad and show the Ad immediately it is loaded successfully.

public class InterstitialAdActivity extends Activity {

    private final String TAG = InterstitialAdActivity.class.getSimpleName();
    private InterstitialAd interstitialAd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Instantiate an InterstitialAd object.
        // NOTE: the placement ID will eventually identify this as your App, you can ignore it for
        // now, while you are testing and replace it later when you have signed up.
        // While you are using this temporary code you will only get test ads and if you release
        // your code like this to the Google Play your users will not receive ads (you will get a no fill error).
        interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
        // Create listeners for the Interstitial Ad
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                // Please refer to Monetization Manager or Reporting API for final impression numbers
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        };

        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAd.loadAd(
                interstitialAd.buildLoadAdConfig()
                        .withAdListener(interstitialAdListener)
                        .build());
    }
}

Interstitial Ads have creatives that are larger in size so a good practice is calling loadAd(...) in advance and then calling show() at the appropriate time.

Scenario 2: Display the ad in a few seconds or minutes after it is successfully loaded. You should check whether the ad has been invalidated before displaying it.

In case of not showing the ad immediately after the ad has been loaded, the developer is responsible for checking whether or not the ad has been invalidated. Once the ad is successfully loaded, the ad will be valid for 60 mins. You will not get paid if you are showing an invalidated ad.

You should follow the idea below, but please do not copy the code into your project since it is just an example:

public class InterstitialAdActivity extends Activity {

    private InterstitialAd  interstitialAd ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Instantiate an InterstitialAd object. 
        // NOTE: the placement ID will eventually identify this as your App, you can ignore it for
        // now, while you are testing and replace it later when you have signed up.
        // While you are using this temporary code you will only get test ads and if you release
        // your code like this to the Google Play your users will not receive ads (you will get a no fill error).
        interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            ...
        };
        // load the ad
        interstitialAd.loadAd(
                interstitialAd.buildLoadAdConfig()
                        .withAdListener(interstitialAdListener)
                        .build());
    }

    private void showAdWithDelay() {
       /**
        * Here is an example for displaying the ad with delay;
        * Please do not copy the Handler into your project
       */
       // Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
           public void run() {
                // Check if interstitialAd has been loaded successfully
               if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
                   return;
               }
                // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
               if(interstitialAd.isAdInvalidated()) {
                   return;
               }
               // Show the ad
                interstitialAd.show(); 
           }
       }, 1000 * 60 * 15); // Show the ad after 15 minutes
    }
}

Finally, add the following code to your Activity's onDestroy() function to release resources the InterstitialAd uses:

@Override
protected void onDestroy() {
    if (interstitialAd != null) {
        interstitialAd.destroy();
    }
    super.onDestroy();
}

Wenn du den standardmäßigen Google Android-Emulator verwendest, füge die folgende Codezeile vor dem Laden einer Testwerbeanzeige hinzu:
AdSettings.addTestDevice("HASHED ID");.

Wenn du zum ersten Mal eine Werbeanzeige für ein Gerät anfragst, verwende die gehashte ID, die im Logcat aufgeführt wird.

Dieser Schritt wird in Genymotion und physischen Geräten nicht benötigt. Wenn du mit echten Werbeanzeigen testen möchtest, lies unseren Testleitfaden.

Start your app and you should see an Interstitial Ad appear:

Hardware-Beschleunigung bei Video Ads

Für Videos Ads im Audience Network muss das Rendering mit Hardware-Beschleunigung aktiviert werden. Andernfalls kann es bei Videoaufrufen zu einem schwarzen Bildschirm kommen. Dies gilt für

  • Videowerbung in Native Ads
  • Videowerbung in Interstitials
  • In-Stream-Video Ads
  • Videos mit Belohnung

Die Hardware-Beschleunigung wird standardmäßig aktiviert, wenn deine Ziel-API-Ebene größer oder gleich 14 ist (Ice Cream Sandwich, Android 4.0.1). Du kannst dieses Feature aber auch explizit auf App-Ebene oder Aktivitätsebene aktivieren.

App-Ebene

Füge in der Android-Manifestdatei das folgende Attribut zum <application>-Tag hinzu, um die Hardware-Beschleunigung für die gesamte App zu aktivieren:

<application android:hardwareAccelerated="true" ...>

Aktivitätsebene

Wenn du das Feature nur für bestimmte Aktivitäten in deiner App aktivieren möchtest, kannst du das folgende Feature in der Android-Manifestdatei zum <activity>-Tag hinzufügen. Im folgenden Beispiel wird die Hardware-Beschleunigung für die AudienceNetworkActivity aktiviert, die für das Rendering von Interstitial Ads und Videos mit Belohnung verwendet wird:

<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>

Next Steps