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 回呼(例如 onResume()onStart() 回呼)中使用 loadAd,因其可能導致在不解除或釋放記憶體中最後一個廣告的情況下一再呼叫 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:秘訣 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 參考資料