loadAd
(橫額廣告除外)。即使您在回呼中收到關於廣告被停用或出錯的通知,也要確保已設定重試次數上限,以免系統無限次重試。無論導致廣告載入失敗的原因為何,系統都會呼叫 onError(Ad, AdError)
。例如,在「沒有互聯網」的情況下,您應在達至重試次數上限時停止載入廣告。
在 Audience Network SDK 中,除了橫額廣告之外,為任何其他廣告類型呼叫 loadAd
時應格外小心。當畫面展示廣告時,切勿呼叫 loadAd
。最好的做法是執行 AdListener
,以在廣告被停用、已完成、被關閉或出錯時收到通知,這樣可讓您再次順利呼叫 loadAd
而不會引起任何問題。
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()); ... }
NativeAd
在 onError
回呼中呼叫 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()); ... }
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()); } }); ... }
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()); }
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 政策與 Facebook 社群守則。深入瞭解我們的審查程序。