在 Android 應用程式中加入插頁廣告

Audience Network 可讓您利用 Facebook 廣告,將 Android 應用程式變成您的營利來源。插頁廣告是可在應用程式中顯示的全螢幕廣告。插頁廣告通常會在應用程式切換畫面時顯示,例如是在遊戲中完成關卡後,或在新聞應用程式中載入報導後。

確保您在開始操作前,已經先行參閱 Audience Network 新手指南Android 新手指南

詳細步驟

第 1 步:在活動中初始化插頁廣告

第 2 步:在活動中顯示插頁廣告

為 Audience Network SDK 執行初始化

Android Audience Network SDK 5.1 版已加入此方法。

若是 5.3.0 或更高版本,您必須初始化 Android 版 Audience Network SDK如欲了解有關初始化 Android 版 Audience Network SDK 的方法,請參閱此文件

在建立廣告物件及載入廣告前,請為 Audience Network SDK 執行初始化。我們建議您在啟動應用程式時執行此動作。

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

第 1 步:在活動中初始化插頁廣告

在活動頂部加入下列程式碼,以匯入 Facebook 廣告 SDK:

import com.facebook.ads.*;

初始化 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");
...  

第 2 步:顯示插頁廣告

情境 1:建立 InterstitialAdListener 後載入廣告,並在廣告載入成功時立即顯示廣告。

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

插頁廣告的廣告創意尺寸比較大,所以最佳做法是提前呼叫 loadAd(...),接著在合適的時間點呼叫 show()

情境 2:在廣告成功載入幾秒或幾分鐘過後顯示廣告。顯示廣告前,您應檢查廣告是否已經無效。

如果廣告在載入後沒有立即顯示,開發人員就需要檢查廣告是否已經無效。成功載入後,廣告的有效期為 60 分鐘。如果您顯示了無效廣告,就無法收取款項

您應該參考下列思路操作,但是請不要將以下程式碼複製到自己的專案中,因為這只是一個範例:

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

最後,在活動的 onDestroy() 函數加入下列程式碼,以釋放 InterstitialAd 使用的資源:

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

如果您正在使用預設的 Google Android 模擬器,則需要在載入測試廣告前加入以下程式碼:
AdSettings.addTestDevice("HASHED ID");

首次執行要求以在裝置中載入廣告時,請使用列在 LogCat 的雜湊編號。

若是 Genymotion 與實體裝置,則可跳過這個步驟。如果您想測試實際廣告,請參閱我們的測試指南

啟動應用程式後,您應該會看到一個插頁廣告:

影片廣告硬件加速

Audience Network 中的影片廣告需要硬件加速顯示才能啟動,否則您的影片畫面可能會出現黑屏。這適用於

  • 原生廣告中的影片廣告創意
  • 插頁廣告中的影片廣告創意
  • 插播影片廣告
  • 獎勵式影片

如果您的 Target API 級別 >=14 (Ice Cream Sandwich, Android 4.0.1),硬件加速就會預設為啟用,但您也可以在應用程式級別或活動級別中啟用此功能。

應用程式級別

在 Android Manifest 檔案中,將下列屬性新增至 <application> 標籤,以啟用整個應用程式的硬件加速:

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

活動級別

如果您只想針對應用程式內的特定活動啟用此功能,請前往 Android Manifest 檔案,將下列功能新增至 <activity> 標籤。下列範例將啟用 AudienceNetworkActivity 的硬件加速功能,以便顯示插頁廣告與獎勵式影片:

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

後續步驟