本指南適用於使用 Google Mobile Ads SDK 整合應用程式開啟頁面廣告的發布商。
應用程式開啟頁面廣告是一種特殊廣告格式,適合想藉由應用程式載入畫面賺取收益的發布商。應用程式開啟頁面廣告會在使用者將應用程式切換至前景時出現,而且使用者隨時可以關閉。
應用程式開啟頁面廣告會在一小塊區域自動顯示品牌資訊,方便使用者判斷目前仍在應用程式內。以下是這類廣告範例:
必要條件
- 完成入門指南的步驟。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。
如要載入測試廣告,最簡單的方法是使用應用程式開啟廣告專用的測試廣告單元 ID:
/21775744923/example/app-open
這類 ID 經特別設定,可針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,將其用於自己的應用程式。發布應用程式前,請務必將這類 ID 替換為自己的廣告單元 ID。
如要進一步瞭解 Google Mobile Ads SDK 測試廣告的運作方式,請參閱「啟用測試廣告」。
擴充 Application 類別
建立擴充 Application
類別的新類別。這項功能提供生命週期感知方式,可管理與應用程式狀態 (而非單一 Activity
) 相關聯的廣告:
Java
public class MyApplication extends Application
implements ActivityLifecycleCallbacks, DefaultLifecycleObserver {
private AppOpenAdManager appOpenAdManager;
private Activity currentActivity;
@Override
public void onCreate() {
super.onCreate();
this.registerActivityLifecycleCallbacks(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
appOpenAdManager = new AppOpenAdManager();
}
Kotlin
class MyApplication :
MultiDexApplication(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {
private lateinit var appOpenAdManager: AppOpenAdManager
private var currentActivity: Activity? = null
override fun onCreate() {
super<MultiDexApplication>.onCreate()
registerActivityLifecycleCallbacks(this)
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
appOpenAdManager = AppOpenAdManager()
}
接著,將下列程式碼新增至 AndroidManifest.xml
:
<!-- TODO: Update to reference your actual package name. -->
<application
android:name="com.google.android.gms.example.appopendemo.MyApplication" ...>
...
</application>
導入公用程式元件
廣告應盡快顯示,最好能在要放送之前預先載入,這樣當使用者一進入應用程式,廣告就能立即出現。
導入公用程式元件 AppOpenAdManager
,封裝與載入及顯示應用程式開啟頁面廣告相關的工作:
Java
private class AppOpenAdManager {
private static final String LOG_TAG = "AppOpenAdManager";
private static final String AD_UNIT_ID = "/21775744923/example/app-open";
private AppOpenAd appOpenAd = null;
private boolean isLoadingAd = false;
private boolean isShowingAd = false;
/** Keep track of the time an app open ad is loaded to ensure you don't show an expired ad. */
private long loadTime = 0;
/** Constructor. */
public AppOpenAdManager() {}
Kotlin
private inner class AppOpenAdManager {
private val googleMobileAdsConsentManager =
private var appOpenAd: AppOpenAd? = null
private var isLoadingAd = false
var isShowingAd = false
/** Keep track of the time an app open ad is loaded to ensure you don't show an expired ad. */
private var loadTime: Long = 0
如要使用 AppOpenAdManager
,請在單例模式 MyApplication
執行個體上呼叫公開包裝函式方法。Application
類別會與其餘程式碼介面互動,將載入及顯示廣告的工作委派給管理工具。
載入廣告
下一步是填寫 loadAd()
方法,並處理廣告載入回呼。
Java
Kotlin
將 AD_UNIT_ID 替換為廣告單元 ID。
顯示廣告
最常見的應用程式開啟頁面廣告導入做法,是在應用程式啟動時嘗試顯示應用程式開啟頁面廣告,如果廣告尚未準備就緒,則啟動應用程式內容,並預先載入另一則廣告,以供下次顯示應用程式開啟頁面廣告。如需導入範例,請參閱應用程式開啟頁面廣告指南。
下列程式碼會顯示廣告,然後重新載入:
Java
public void showAdIfAvailable(
@NonNull final Activity activity,
@NonNull OnShowAdCompleteListener onShowAdCompleteListener) {
// If the app open ad is already showing, do not show the ad again.
if (isShowingAd) {
Log.d(TAG, "The app open ad is already showing.");
return;
}
// If the app open ad is not available yet, invoke the callback then load the ad.
if (appOpenAd == null) {
Log.d(TAG, "The app open ad is not ready yet.");
onShowAdCompleteListener.onShowAdComplete();
// Load an ad.
return;
}
isShowingAd = true;
appOpenAd.show(activity);
}
Kotlin
fun showAdIfAvailable(activity: Activity, onShowAdCompleteListener: OnShowAdCompleteListener) {
// If the app open ad is already showing, do not show the ad again.
if (isShowingAd) {
Log.d(TAG, "The app open ad is already showing.")
return
}
// If the app open ad is not available yet, invoke the callback then load the ad.
if (appOpenAd == null) {
Log.d(TAG, "The app open ad is not ready yet.")
onShowAdCompleteListener.onShowAdComplete()
// Load an ad.
return
}
isShowingAd = true
appOpenAd?.show(activity)
}
設定 FullScreenContentCallback
FullScreenContentCallback
會處理與顯示 AppOpenAd
相關的事件,顯示 AppOpenAd
前,請務必設定回呼:
Java
appOpenAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when full screen content is dismissed.
Log.d(TAG, "Ad dismissed fullscreen content.");
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
appOpenAd = null;
isShowingAd = false;
onShowAdCompleteListener.onShowAdComplete();
// Load an ad.
}
@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
// Called when full screen content failed to show.
Log.d(TAG, adError.getMessage());
appOpenAd = null;
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
isShowingAd = false;
onShowAdCompleteListener.onShowAdComplete();
// Load an ad.
}
@Override
public void onAdShowedFullScreenContent() {
Log.d(TAG, "Ad showed fullscreen content.");
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "The ad recorded an impression.");
}
@Override
public void onAdClicked() {
// Called when ad is clicked.
Log.d(TAG, "The ad was clicked.");
}
});
Kotlin
appOpenAd?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Called when full screen content is dismissed.
Log.d(TAG, "Ad dismissed fullscreen content.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
// Load an ad.
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when full screen content failed to show.
Log.d(TAG, adError.message)
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
// Load an ad.
}
override fun onAdShowedFullScreenContent() {
Log.d(TAG, "Ad showed fullscreen content.")
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "The ad recorded an impression.")
}
override fun onAdClicked() {
// Called when ad is clicked.
Log.d(TAG, "The ad was clicked.")
}
}
考慮廣告效期
為避免顯示失效的廣告,請在 AppOpenAdManager
中加入方法,檢查廣告參照載入後經過的時間。再使用該方法檢查廣告是否仍有效。
Java
/** Check if ad was loaded more than n hours ago. */
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
long dateDifference = (new Date()).getTime() - loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * numHours));
}
/** Check if ad exists and can be shown. */
private boolean isAdAvailable() {
// For time interval details, see: https://support.google.com/admob/answer/9341964
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
}
Kotlin
/** Check if ad was loaded more than n hours ago. */
private fun wasLoadTimeLessThanNHoursAgo(numHours: Long): Boolean {
val dateDifference: Long = Date().time - loadTime
val numMilliSecondsPerHour: Long = 3600000
return dateDifference < numMilliSecondsPerHour * numHours
}
/** Check if ad exists and can be shown. */
private fun isAdAvailable(): Boolean {
// For time interval details, see: https://support.google.com/admob/answer/9341964
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4)
}
追蹤目前的活動
如要顯示廣告,您需要 Activity
情境。如要追蹤目前使用的最新活動,請註冊並實作 Application.ActivityLifecycleCallbacks
。
Java
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}
@Override
public void onActivityStarted(@NonNull Activity activity) {
// An ad activity is started when an ad is showing, which could be AdActivity class from Google
// SDK or another activity class implemented by a third party mediation partner. Updating the
// currentActivity only when an ad is not showing will ensure it is not an ad activity, but the
// one that shows the ad.
if (!appOpenAdManager.isShowingAd) {
currentActivity = activity;
}
}
@Override
public void onActivityResumed(@NonNull Activity activity) {}
@Override
public void onActivityPaused(@NonNull Activity activity) {}
@Override
public void onActivityStopped(@NonNull Activity activity) {}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {}
Kotlin
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
override fun onActivityStarted(activity: Activity) {
// An ad activity is started when an ad is showing, which could be AdActivity class from Google
// SDK or another activity class implemented by a third party mediation partner. Updating the
// currentActivity only when an ad is not showing will ensure it is not an ad activity, but the
// one that shows the ad.
if (!appOpenAdManager.isShowingAd) {
currentActivity = activity
}
}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}
registerActivityLifecycleCallbacks
可監聽所有 Activity
事件。監聽活動的啟動和終止時間,即可追蹤目前 Activity
的參照,然後用於顯示應用程式開啟廣告。
監聽應用程式前景事件
如要監聽應用程式前景事件,請按照下列步驟操作:
將程式庫加到 Gradle 檔案
如要接收應用程式前景事件通知,請註冊 DefaultLifecycleObserver
。將其依附元件新增至應用程式層級的建構檔:
Kotlin
dependencies { implementation("com.google.android.gms:play-services-ads:24.5.0") implementation("androidx.lifecycle:lifecycle-process:2.8.3") }
Groovy
dependencies { implementation 'com.google.android.gms:play-services-ads:24.5.0' implementation 'androidx.lifecycle:lifecycle-process:2.8.3' }
導入生命週期觀察器介面
您可以實作 DefaultLifecycleObserver
介面,監聽前景事件。
導入 onStart()
,顯示應用程式開啟頁面廣告。
Java
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
appOpenAdManager.showAdIfAvailable(currentActivity);
}
Kotlin
override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
currentActivity?.let {
// Show the ad (if available) when the app moves to foreground.
appOpenAdManager.showAdIfAvailable(it)
}
}
冷啟動和載入畫面
截至目前,本文假設的應用程式開啟頁面廣告開始時機,均為「使用者將暫存於記憶體中的應用程式恢復到前景時」。如果應用程式啟動時,不是從記憶體中的暫存狀態恢復到前景,即所謂的「冷啟動」。
舉例來說,使用者初次開啟應用程式就是冷啟動,這時不會有預先載入的應用程式開啟頁面廣告,因此無法立即顯示。從請求到收到廣告之間會有延遲,使用者在這一小段期間可使用應用程式,但接著會突然看到與內容無關的廣告。這種情況會造成使用者體驗不佳,建議避免。
要在冷啟動時放送應用程式開啟頁面廣告,建議使用載入畫面,載入遊戲或應用程式素材資源,並只在載入畫面中顯示廣告。如果應用程式已完成載入,並將使用者帶往應用程式的主要內容,則請勿顯示廣告。
最佳做法
應用程式開啟頁面廣告可協助您在應用程式首次啟動和切換期間,透過應用程式載入畫面營利,但請務必採行最佳做法,確保使用者有良好的應用程式體驗。我們建議您:
- 等使用者用過應用程式幾次之後,再放送第一則應用程式開啟頁面廣告。
- 在使用者原本會等待應用程式載入的時間內,放送應用程式開啟頁面廣告。
- 如果應用程式開啟頁面廣告下方有載入畫面,且該畫面在廣告關閉前就載入完畢,建議以
onAdDismissedFullScreenContent()
方法關閉載入畫面。
GitHub 上的範例
後續步驟
請參閱下列主題: