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