L’Audience Network vous permet de monétiser vos applications Android avec des publicités Facebook. Une publicité interstitielle est une publicité plein écran que vous pouvez diffuser au sein de votre application. Généralement, les publicités interstitielles sont diffusées lors d’une transition au sein de votre application. Par exemple, après avoir atteint un niveau dans un jeu ou chargé une actualité dans une application d’actualités.
Assurez-vous d’avoir terminé le Guide de démarrage et le guide Démarrer avec Android avant de commencer.
La méthode a été ajoutée à la version 5.1 du SDK Audience Network pour Android.
L’initialisation explicite du SDK Audience Network pour Android est requise à partir de la version 5.3.0. Veuillez consulter ce document pour savoir comment initialiser le SDK Audience Network pour Android.
Avant de télécharger un objet publicitaire et de charger des publicités, vous devez initialiser le SDK Audience Network. Il est recommandé de le faire lors du démarrage de l’application.
public class YourApplication extends Application {
...
@Override
public void onCreate() {
super.onCreate();
// Initialize the Audience Network SDK
AudienceNetworkAds.initialize(this);
}
...
}Pour importer le SDK Publicités Facebook, ajoutez le code suivant en haut de votre activité :
import com.facebook.ads.*;
Initialisez 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");
... InterstitialAdListener, chargement de la publicité et affichage de la publicité immédiatement après son chargement.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());
}
}Les publicités interstitielles contiennent des contenus plus larges. Il est donc recommandé d’appeler loadAd(...) à l’avance, puis d’appeler show() au moment opportun.
Si la publicité ne s’affiche pas immédiatement après avoir été chargée, le développeur ou la développeuse doit vérifier si elle n’a pas été invalidée. Une fois la publicité chargée, elle est valide pendant 60 minutes. Vous ne serez pas payé·e si vous diffusez une publicité invalidée.
Suivez l’idée ci-dessous, mais ne copiez surtout pas le code dans votre projet, car il s’agit d’un exemple :
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
}
}Enfin, ajoutez le code suivant à la fonction onDestroy() de votre activité pour publier les ressources utilisées par InterstitialAd :
@Override
protected void onDestroy() {
if (interstitialAd != null) {
interstitialAd.destroy();
}
super.onDestroy();
}Si vous utilisez l’émulateur Google Android par défaut, ajoutez la ligne de code suivante avant de charger une publicité test :AdSettings.addTestDevice("HASHED ID");.
Utilisez l’ID haché imprimé sur le logcat la première fois que vous effectuez une demande de chargement d’une publicité sur un appareil.
Cette étape n’est pas nécessaire pour Genymotion ni pour les appareils physiques. Si vous souhaitez effectuer un test avec de vraies publicités, consultez notre guide de test.
Lancez votre application, et une publicité interstitielle doit s’afficher :

Les publicités vidéo dans l’Audience Network nécessitent le rendu par accélération matérielle pour être activées. Elles risquent autrement d’afficher un écran noir. Cela concerne les :
L’accélération matérielle est activée par défaut si votre niveau d’API Target est >=14 (Ice Cream Sandwich, Android 4.0.1). Vous pouvez toutefois activer explicitement cette fonction au niveau de l’application ou de l’activité.
Dans votre fichier manifeste Android, ajoutez l’attribut suivant à la balise <application> afin de permettre l’accélération matérielle pour toute votre application :
<application android:hardwareAccelerated="true" ...>
Si vous ne souhaitez activer la fonction que pour certaines activités dans votre application, ajoutez la fonction suivante à la balise <activity> dans votre manifeste Android. L’exemple suivant permettra l’accélération matérielle pour le AudienceNetworkActivity qui est utilisé pour le rendu des publicités interstitielles et des vidéos avec récompense :
<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>
Des exemples de code pertinents Swift et Objective-C sont disponibles dans notre répertoire d’exemple d’application sur GitHub
Testez l’intégration de vos publicités à votre application.
Dès que nous recevrons une demande de publicité de votre application ou de votre site Web, nous l’examinerons afin de vérifier qu’elle est conforme aux règles de l’Audience Network et aux Standards de la communauté Facebook.