插頁式廣告

插頁式廣告會全螢幕顯示,覆蓋整個應用程式流程的介面。這類廣告通常會在應用程式流程中的自然轉換點顯示,例如活動之間或遊戲關卡之間的暫停時間。 應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或關閉廣告並返回應用程式。 閱讀我們的個案研究

本指南說明如何將插頁式廣告整合至 Android 應用程式。

必要條件

  • Google Mobile Ads SDK 19.7.0 以上版本。
  • 完成入門指南

一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非正式版廣告。否則可能導致帳戶遭到停權。

如要載入測試廣告,最簡單的方法是使用 Android 插頁式廣告的專屬測試廣告單元 ID:

ca-app-pub-3940256099942544/1033173712

我們已特別將每個請求設為傳回測試廣告;編寫程式碼、測試及偵錯時,您可以在自己的應用程式中自由使用這項工具。但請注意,發布應用程式前,請務必先將它換成自己的廣告單元 ID。

如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」一文。

Load an ad

To load an interstitial ad, call the InterstitialAd static load() method and pass in an InterstitialAdLoadCallback to receive the loaded ad or any possible errors. Notice that like other format load callbacks, InterstitialAdLoadCallback leverages LoadAdError to provide higher fidelity error details.

Java

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends Activity {

  private InterstitialAd mInterstitialAd;
  private static final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
        new InterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;
        Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.d(TAG, loadAdError.toString());
        mInterstitialAd = null;
      }
    });
  }
}

Kotlin

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

class MainActivity : AppCompatActivity() {

  private var mInterstitialAd: InterstitialAd? = null
  private final val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      var adRequest = AdRequest.Builder().build()

      InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
        override fun onAdFailedToLoad(adError: LoadAdError) {
          Log.d(TAG, adError?.toString())
          mInterstitialAd = null
        }

        override fun onAdLoaded(interstitialAd: InterstitialAd) {
          Log.d(TAG, 'Ad was loaded.')
          mInterstitialAd = interstitialAd
        }
      })
    }
}

Set the FullScreenContentCallback

The FullScreenContentCallback handles events related to displaying your InterstitialAd. Before showing InterstitialAd, make sure to set the callback:

Java

mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
  @Override
  public void onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.");
  }

  @Override
  public void onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.");
    mInterstitialAd = null;
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.");
    mInterstitialAd = null;
  }

  @Override
  public void onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.");
  }

  @Override
  public void onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.");
  }
});

Kotlin

mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.")
  }

  override fun onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    Log.d(TAG, "Ad dismissed fullscreen content.")
    mInterstitialAd = null
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.")
    mInterstitialAd = null
  }

  override fun onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.")
  }

  override fun onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.")
  }
}

Show the ad

Interstitial ads should be displayed during natural pauses in the flow of an app. Between levels of a game is a good example, or after the user completes a task. To show an interstitial, use the show() method.

Java

if (mInterstitialAd != null) {
  mInterstitialAd.show(MyActivity.this);
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.");
}

Kotlin

if (mInterstitialAd != null) {
  mInterstitialAd?.show(this)
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.")
}

一些最佳做法

請思考插頁式廣告是否為適合您應用程式的廣告類型。
插頁式廣告最適合用於包含自然轉換點的應用程式。 應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會產生這類時間點。請務必考慮在應用程式工作流程中的哪個時間點顯示插頁式廣告,以及使用者的可能反應。
請記得在顯示插頁式廣告時暫停動作。
插頁式廣告有多種類型:文字、圖像、影片等。請務必確保應用程式顯示插頁式廣告時,一併暫停使用部分資源,廣告才能利用資源。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。
允許充分的載入時間。
確保在適當時機顯示插頁式廣告的重要性,您也應該確保使用者不必等待廣告載入。在您想呼叫 show() 之前呼叫 load() 即可事先載入廣告,可確保應用程式在顯示時已有完全載入的插頁式廣告。
不要讓應用程式廣告氾濫。
增加在應用程式中顯示插頁式廣告的頻率也許是提升收益的好方法,但也有可能導致使用者體驗和點閱率下降。請確保使用者不會經常中斷,以免他們無法再使用您的應用程式。

原始碼

GitHub 上的範例

成功案例

後續步驟