Android 的 Audience Network SDK 整合貼士

以下是一些為開發人員執行而設的 Android 整合最佳操作實例。這些編碼貼士和場景將有助您的團隊更深入了解我們的 Audience Network SDK,使整合流程更具效率,並使您的解決方案在發佈後保持最新狀態。

必要條件

請確保您已完成以下其中一個範例:

問題:因多次呼叫 loadAd 而造成的當機情況。

良好操作實例

不良操作實例


良好操作實例:

貼士 1:只在廣告被停用或廣告出現載入錯誤時才再次呼叫 loadAd(橫額廣告除外)。

即使您在回呼中收到關於廣告被停用或出錯的通知,也要確保已設定重試次數上限,以免系統無限次重試。無論導致廣告載入失敗的原因為何,系統都會呼叫 onError(Ad, AdError)。例如,在「沒有互聯網」的情況下,您應在達至重試次數上限時停止載入廣告。

在 Audience Network SDK 中,除了橫額廣告之外,為任何其他廣告類型呼叫 loadAd 時應格外小心。當畫面展示廣告時,切勿呼叫 loadAd。最好的做法是執行 AdListener,以在廣告被停用、已完成、被關閉或出錯時收到通知,這樣可讓您再次順利呼叫 loadAd 而不會引起任何問題。

範例 1:在 onInterstitialDismissed 回呼中呼叫 loadAd

private InterstitialAd mInterstitialAd;
private static final MAX_NUMBER_OF_RETRIES = 3;
private boolean shouldLoadAd = true;
private int retryCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onCreate 
    super.onCreate(savedInstanceState);
    ...
    mInterstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
    InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
        ...
        @Override
        public void onInterstitialDismissed(Ad ad) {
            // Interstitial dismissed callback
            if (shouldLoadAd) {
                /* Change shouldLoadAd value to false, 
                    or a new interstitial ad will show immediately 
                    when previous interstitial ad gets dismissed. */
                shouldLoadAd = false;
                mInterstitialAd.loadAd();
            }
        }
        
        @Override
        public void onError(Ad ad, AdError adError) {
            // Ad error callback
            // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES
            if(retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) {
                retryCount += 1;
                mInterstitialAd.loadAd();
            }
        }
        ...
    };
    mInterstitialAd.loadAd(
            mInterstitialAd.buildLoadAdConfig()
                    .withAdListener(interstitialAdListener)
                    .build());
    ...
}

範例 2:為 NativeAdonError 回呼中呼叫 loadAd。

private static final MAX_NUMBER_OF_RETRIES = 3;
private NativeAd mNativeAd;
private int retryCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    NativeAdListener nativeAdListener = new AdListener() {
        ...
        @Override
        public void onError(Ad ad, AdError error) {
            // Ad error callback
            // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES
            if (retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) {
                retryCount += 1;
                mNativeAd.loadAd();
            }
        }
        ...
    };
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(nativeAdListener)
                    .build());
    ...
}

貼士 2:廣告實例並非廣告管理員。每當您需要為原生廣告和橫額廣告「重新載入」廣告時,請產生新的實例。

範例:

private Button showNativeAdButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    showNativeAdButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Create a native ad request with a placement ID
            nativeAd = new NativeAd(NativeAdSampleActivity.this, "YOUR_PLACEMENT_ID");

            // Create a listener to get notified when the ad was loaded.
            NativeAdListener nativeAdListener = new AdListener() {
                ...
            }

            // Initiate a request to load an ad.
            nativeAd.loadAd(
                    nativeAd.buildLoadAdConfig()
                            .withAdListener(nativeAdListener)
                            .build());
        }
    });
    ...
}

不良操作實例:

貼士 1:請勿在部分 Android 回呼中使用 loadAd(如 onResume()onStart() 回呼),這可能會導致系統反覆呼叫 loadAd,而不停用或釋放記憶體中的上一個廣告。


範例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    ...
}

@Override
protected void onResume() {
    super.onResume();
    ...
    // DON'T DO THIS!
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(NativeAdSampleActivity.this)
                    .build());
}

貼士 2:小心不要在 onError 回呼中無限次呼叫 loadAd。以下是反面範例。在「沒有互聯網」的情況下反覆呼叫 loadAd,最終導致應用程式出現當機情況。


範例:

private NativeAd mNativeAd;
private boolean shouldRetry = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // call the super class onResume
    super.onCreate(savedInstanceState);
    ...
    mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    NativeAdListener nativeAdListener = new AdListener() {
        ...
        @Override
        public void onError(Ad ad, AdError error) {
            // Ad error callback
            // Remember to change shouldRetry to false, or DON'T implement like this! 
            if (shouldRetry) {
                mNativeAd.loadAd();
            }
        }
        ...
    };
    mNativeAd.loadAd(
            mNativeAd.buildLoadAdConfig()
                    .withAdListener(NativeAdSampleActivity.this)
                    .build());
    ...
}

後續步驟

更多資源

新手指南

Audience Network 入門技術指南

API 參考資料

Facebook iOS SDK 參考資料