Quảng cáo khi mở ứng dụng

Tài liệu hướng dẫn này dành cho những nhà xuất bản tích hợp quảng cáo khi mở ứng dụng bằng SDK Quảng cáo trên thiết bị di động của Google (bản thử nghiệm).

Quảng cáo khi mở ứng dụng là một định dạng quảng cáo đặc biệt dành cho những nhà xuất bản muốn kiếm tiền từ màn hình tải ứng dụng của họ. Quảng cáo khi mở ứng dụng được thiết kế để xuất hiện khi người dùng chạy ứng dụng của bạn trên nền trước và họ có thể đóng quảng cáo này bất cứ lúc nào.

Quảng cáo khi mở ứng dụng tự động hiển thị một vùng nhỏ chứa thông tin thương hiệu để người dùng biết họ đang mở ứng dụng của bạn. Dưới đây là một ví dụ về cách hiển thị của quảng cáo khi mở ứng dụng:

Điều kiện tiên quyết

Luôn thử nghiệm bằng quảng cáo thử nghiệm

Khi bạn tạo và thử nghiệm ứng dụng, hãy nhớ sử dụng quảng cáo thử nghiệm thay vì quảng cáo đang chạy trong thực tế. Chúng tôi có thể tạm ngưng tài khoản của bạn nếu bạn không làm như vậy.

Cách dễ nhất để tải quảng cáo thử nghiệm là dùng mã đơn vị quảng cáo thử nghiệm chúng tôi dành riêng cho quảng cáo khi mở ứng dụng:

ca-app-pub-3940256099942544/9257395921

Mã này được định cấu hình đặc biệt để trả về quảng cáo thử nghiệm cho mọi yêu cầu, và bạn có thể sử dụng mã này trong ứng dụng của mình khi lập trình, chạy thử nghiệm và gỡ lỗi. Bạn chỉ cần nhớ thay thế mã này bằng mã đơn vị quảng cáo của mình trước khi xuất bản ứng dụng.

Để biết thêm thông tin về cách hoạt động của quảng cáo thử nghiệm của SDK Quảng cáo của Google trên thiết bị di động (beta), hãy xem bài viết Bật quảng cáo thử nghiệm.

Mở rộng lớp Ứng dụng

Tạo một lớp mới mở rộng lớp Application. Điều này cung cấp một cách thức nhận biết vòng đời để quản lý những quảng cáo được liên kết với trạng thái của ứng dụng thay vì một Activity duy nhất:

Kotlin

/** Application class that initializes, loads and show ads when activities change states. */
class MyApplication : Application() {

  override fun onCreate() {
    super<Application>.onCreate()
    CoroutineScope(Dispatchers.IO).launch {
      // Initialize the Mobile Ads SDK synchronously on a background thread.
      MobileAds.initialize(this@MyApplication, InitializationConfig.Builder(APP_ID).build()) {}
    }
  }

  private companion object {
    // Sample AdMob App ID.
    const val APP_ID = "ca-app-pub-3940256099942544~3347511713"
  }
}

Java

/** Application class that initializes, loads and show ads when activities change states. */
public class MyApplication extends Application {

  // Sample AdMob App ID.
  private static final String APP_ID = "ca-app-pub-3940256099942544~3347511713";

  @Override
  public void onCreate() {
    super.onCreate();
    new Thread(
        () -> {
          // Initialize the SDK on a background thread.
          MobileAds.initialize(
              MyApplication.this,
              new InitializationConfig.Builder(APP_ID).build(),
              initializationStatus -> {});
        })
        .start();
  }
}

Thao tác này sẽ cung cấp bộ khung mà sau này bạn sẽ dùng để đăng ký các sự kiện đưa ứng dụng lên nền trước.

Tiếp theo, hãy thêm mã sau vào AndroidManifest.xml của bạn:

<!-- TODO: Update to reference your actual package name. -->
<application
    android:name="com.google.android.gms.example.appopendemo.MyApplication" ...>
...
</application>

Triển khai thành phần tiện ích

Quảng cáo của bạn cần phải nhanh chóng hiển thị. Do đó, tốt nhất là bạn nên tải quảng cáo trước khi cần hiển thị quảng cáo. Khi bạn làm như vậy, quảng cáo sẽ sẵn sàng hoạt động ngay khi người dùng truy cập vào ứng dụng của bạn.

Triển khai một thành phần tiện ích AppOpenAdManager để đóng gói công việc liên quan đến việc tải và hiển thị quảng cáo khi mở ứng dụng:

Kotlin

/**
* Interface definition for a callback to be invoked when an app open ad is complete (i.e. dismissed
* or fails to show).
*/
fun interface OnShowAdCompleteListener {
  fun onShowAdComplete()
}

/** Singleton object that loads and shows app open ads. */
object AppOpenAdManager {
  private var appOpenAd: AppOpenAd? = null
  private var isLoadingAd = false
  var isShowingAd = false

  /**
  * Load an ad.
  *
  * @param context a context used to perform UI-related operations (e.g. display Toast messages).
  *   Showing the app open ad itself does not require a context.
  */
  fun loadAd(context: Context) {
    // We will implement this later.
  }

  /**
  * Show the ad if one isn't already showing.
  *
  * @param activity the activity that shows the app open ad.
  * @param onShowAdCompleteListener the listener to be notified when an app open ad is complete.
  */
  fun showAdIfAvailable(activity: Activity, onShowAdCompleteListener: OnShowAdCompleteListener?) {
    // We will implement this later.
  }

  /** Check if ad exists and can be shown. */
  private fun isAdAvailable(): Boolean {
    return appOpenAd != null
  }
}

Java

/** Singleton object that loads and shows app open ads. */
public class AppOpenAdManager {

  /**
  * Interface definition for a callback to be invoked when an app open ad is complete (i.e.
  * dismissed or fails to show).
  */
  public interface OnShowAdCompleteListener {
    void onShowAdComplete();
  }

  private static AppOpenAdManager instance;
  private AppOpenAd appOpenAd;
  private boolean isLoadingAd = false;
  private boolean isShowingAd = false;

  /** Keep track of the time an app open ad is loaded to make sure you don't show an expired ad. */
  private long loadTime = 0;

  public static synchronized AppOpenAdManager getInstance() {
    if (instance == null) {
      instance = new AppOpenAdManager();
    }
    return instance;
  }

  /**
  * Load an ad.
  *
  * @param context a context used to perform UI-related operations (e.g. display Toast messages).
  *     Loading the app open ad itself does not require a context.
  */
  public void loadAd(@NonNull Context context) {
    // We will implement this later.
  }

  /**
  * Show the ad if one isn't already showing.
  *
  * @param activity the activity that shows the app open ad.
  * @param onShowAdCompleteListener the listener to be notified when an app open ad is complete.
  */
  public void showAdIfAvailable(
      @NonNull Activity activity, @Nullable OnShowAdCompleteListener onShowAdCompleteListener) {
    // We will implement this later.
  }

  /** Check if ad exists and can be shown. */
  private boolean isAdAvailable() {
    return appOpenAd != null
  }
}

Bây giờ, khi đã có lớp tiện ích, bạn có thể tạo bản sao lớp này trong lớp MyApplication của mình:

Java

public class MyApplication extends Application {

  private AppOpenAdManager appOpenAdManager;

  @Override
  public void onCreate() {
    super.onCreate();
    new Thread(
            () -> {
              // Initialize Google Mobile Ads SDK (beta) on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});
            })
        .start();
    appOpenAdManager = new AppOpenAdManager(this);
  }
}

Kotlin

class MyApplication : Application() {

  private lateinit var appOpenAdManager: AppOpenAdManager

  override fun onCreate() {
    super.onCreate()
    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize Google Mobile Ads SDK (beta) on a background thread.
      MobileAds.initialize(this@MyApplication) {}
    }
    appOpenAdManager = AppOpenAdManager()
  }
}

Để sử dụng AppOpenAdManager, hãy gọi các phương thức trình bao bọc công khai trên thực thể singleton MyApplication. Lớp Application tương tác với phần còn lại của mã, uỷ quyền công việc tải và hiển thị quảng cáo cho trình quản lý.

Tải một quảng cáo

Bước tiếp theo là điền vào phương thức loadAd() và xử lý các lệnh gọi lại tải quảng cáo.

Kotlin


/**
 * Load an ad.
 *
 * @param context a context used to perform UI-related operations (e.g. display Toast messages).
 *   Loading the app open ad itself does not require a context.
 */
fun loadAd(context: Context) {
  // Do not load ad if there is an unused ad or one is already loading.
  if (isLoadingAd || isAdAvailable()) {
    Log.d(Constant.TAG, "App open ad is either loading or has already loaded.")
    return
  }

  isLoadingAd = true
  AppOpenAd.load(
    AdRequest.Builder(AppOpenFragment.AD_UNIT_ID).build(),
    object : AdLoadCallback<AppOpenAd> {
      /**
       * Called when an app open ad has loaded.
       *
       * @param ad the loaded app open ad.
       */
      override fun onAdLoaded(ad: AppOpenAd) {
        // Called when an ad has loaded.
        appOpenAd = ad
        isLoadingAd = false
        Log.d(Constant.TAG, "App open ad loaded.")
      }

      /**
       * Called when an app open ad has failed to load.
       *
       * @param loadAdError the error.
       */
      override fun onAdFailedToLoad(loadAdError: LoadAdError) {
        isLoadingAd = false
        Log.w(Constant.TAG, "App open ad failed to load: $loadAdError")
      }
    },
  )
}

Java


/**
 * Load an ad.
 *
 * @param context a context used to perform UI-related operations (e.g. display Toast messages).
 *     Loading the app open ad itself does not require a context.
 */
public void loadAd(@NonNull Context context) {
  // Do not load ad if there is an unused ad or one is already loading.
  if (isLoadingAd || isAdAvailable()) {
    Log.d(Constant.TAG, "App open ad is either loading or has already loaded.");
    return;
  }

  isLoadingAd = true;
  AppOpenAd.load(
      new AdRequest.Builder(AppOpenFragment.AD_UNIT_ID).build(),
      new AdLoadCallback<AppOpenAd>() {
        @Override
        public void onAdLoaded(@NonNull AppOpenAd ad) {
          appOpenAd = ad;
          isLoadingAd = false;
          Log.d(Constant.TAG, "App open ad loaded.");
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          isLoadingAd = false;
          Log.w(Constant.TAG, "App open ad failed to load: " + loadAdError);
        }
      });
}

Thay thế AD_UNIT_ID bằng mã đơn vị quảng cáo của riêng bạn.

Hiển thị quảng cáo

Cách triển khai quảng cáo khi mở ứng dụng phổ biến nhất là cố gắng hiển thị quảng cáo khi mở ứng dụng gần thời điểm khởi chạy ứng dụng, bắt đầu nội dung ứng dụng nếu quảng cáo chưa sẵn sàng và tải trước một quảng cáo khác cho cơ hội mở ứng dụng tiếp theo. Xem Hướng dẫn về quảng cáo khi mở ứng dụng để biết các ví dụ về cách triển khai.

Đoạn mã sau đây cho thấy và sau đó tải lại một quảng cáo:

Kotlin

/**
 * Show the ad if one isn't already showing.
 *
 * @param activity the activity that shows the app open ad.
 * @param onShowAdCompleteListener the listener to be notified when an app open ad is complete.
 */
fun showAdIfAvailable(activity: Activity, onShowAdCompleteListener: OnShowAdCompleteListener?) {
  // If the app open ad is already showing, do not show the ad again.
  if (isShowingAd) {
    Log.d(Constant.TAG, "App open ad is already showing.")
    onShowAdCompleteListener?.onShowAdComplete()
    return
  }

  // If the app open ad is not available yet, invoke the callback.
  if (!isAdAvailable()) {
    Log.d(Constant.TAG, "App open ad is not ready yet.")
    onShowAdCompleteListener?.onShowAdComplete()
    return
  }

  appOpenAd?.adEventCallback =
    object : AppOpenAdEventCallback {
      override fun onAdShowedFullScreenContent() {
        Log.d(Constant.TAG, "App open ad showed.")
      }

      override fun onAdDismissedFullScreenContent() {
        Log.d(Constant.TAG, "App open ad dismissed.")
        appOpenAd = null
        isShowingAd = false
        onShowAdCompleteListener?.onShowAdComplete()
        loadAd(activity)
      }

      override fun onAdFailedToShowFullScreenContent(
        fullScreenContentError: FullScreenContentError
      ) {
        appOpenAd = null
        isShowingAd = false
        Log.w(Constant.TAG, "App open ad failed to show: $fullScreenContentError")
        onShowAdCompleteListener?.onShowAdComplete()
        loadAd(activity)
      }

      override fun onAdImpression() {
        Log.d(Constant.TAG, "App open ad recorded an impression.")
      }

      override fun onAdClicked() {
        Log.d(Constant.TAG, "App open ad recorded a click.")
      }
    }

  isShowingAd = true
  appOpenAd?.show(activity)
}

Java

/**
 * Show the ad if one isn't already showing.
 *
 * @param activity the activity that shows the app open ad.
 * @param onShowAdCompleteListener the listener to be notified when an app open ad is complete.
 */
public void showAdIfAvailable(
    @NonNull Activity activity, @Nullable OnShowAdCompleteListener onShowAdCompleteListener) {
  // If the app open ad is already showing, do not show the ad again.
  if (isShowingAd) {
    Log.d(Constant.TAG, "App open ad is already showing.");
    if (onShowAdCompleteListener != null) {
      onShowAdCompleteListener.onShowAdComplete();
    }
    return;
  }

  // If the app open ad is not available yet, invoke the callback.
  if (!isAdAvailable()) {
    Log.d(Constant.TAG, "App open ad is not ready yet.");
    if (onShowAdCompleteListener != null) {
      onShowAdCompleteListener.onShowAdComplete();
    }
    return;
  }

  appOpenAd.setAdEventCallback(
      new AppOpenAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          Log.d(Constant.TAG, "App open ad shown.");
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          Log.d(Constant.TAG, "App open ad dismissed.");
          appOpenAd = null;
          isShowingAd = false;
          if (onShowAdCompleteListener != null) {
            onShowAdCompleteListener.onShowAdComplete();
          }
          loadAd(activity);
        }

        @Override
        public void onAdFailedToShowFullScreenContent(
            @NonNull FullScreenContentError fullScreenContentError) {
          appOpenAd = null;
          isShowingAd = false;
          Log.w(Constant.TAG, "App open ad failed to show: " + fullScreenContentError);
          if (onShowAdCompleteListener != null) {
            onShowAdCompleteListener.onShowAdComplete();
          }
          loadAd(activity);
        }

        @Override
        public void onAdImpression() {
          Log.d(Constant.TAG, "App open ad recorded an impression.");
        }

        @Override
        public void onAdClicked() {
          Log.d(Constant.TAG, "App open ad recorded a click.");
        }
      });

  isShowingAd = true;
  appOpenAd.show(activity);
}

AppOpenAdEventCallback xử lý các sự kiện như khi quảng cáo hiển thị, không hiển thị hoặc khi bị loại bỏ.

Xem xét thời hạn của quảng cáo

Để đảm bảo bạn không hiển thị quảng cáo đã hết hạn, hãy thêm một phương thức vào AppOpenAdManager nhằm kiểm tra thời lượng kể từ khi tải tệp tham chiếu quảng cáo. Sau đó, hãy dùng phương thức đó để kiểm tra xem quảng cáo có còn hợp lệ hay không.

Kotlin

object AppOpenAdManager {
  // ...
  /** Keep track of the time an app open ad is loaded to make sure you don't show an expired ad. */
  private var loadTime: Long = 0;
  
  /**
   * Load an ad.
   *
   * @param context a context used to perform UI-related operations (e.g. display Toast messages).
   *   Loading the app open ad itself does not require a context.
   */
  fun loadAd(context: Context) {
    // Do not load ad if there is an unused ad or one is already loading.
    if (isLoadingAd || isAdAvailable()) {
      Log.d(Constant.TAG, "App open ad is either loading or has already loaded.")
      return
    }

    isLoadingAd = true
    AppOpenAd.load(
      AdRequest.Builder(AppOpenFragment.AD_UNIT_ID).build(),
      object : AdLoadCallback<AppOpenAd> {
        /**
         * Called when an app open ad has loaded.
         *
         * @param ad the loaded app open ad.
         */
        override fun onAdLoaded(ad: AppOpenAd) {
          // Called when an ad has loaded.
          appOpenAd = ad
          isLoadingAd = false
          loadTime = Date().time
          Log.d(Constant.TAG, "App open ad loaded.")
        }

        /**
         * Called when an app open ad has failed to load.
         *
         * @param loadAdError the error.
         */
        override fun onAdFailedToLoad(loadAdError: LoadAdError) {
          isLoadingAd = false
          Log.w(Constant.TAG, "App open ad failed to load: $loadAdError")
        }
      },
    )
  }

  // ...

  /** 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 {
    // App open ads expire after four hours. Ads rendered more than four hours after request time
    // are no longer valid and may not earn revenue.
    return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4)
  }
}

Java

public class AppOpenAdManager {
  // ...
  /** Keep track of the time an app open ad is loaded to make sure you don't show an expired ad. */
  private long loadTime = 0;
  
  /**
   * Load an ad.
   *
   * @param context a context used to perform UI-related operations (e.g. display Toast messages).
   *     Loading the app open ad itself does not require a context.
   */
  public void loadAd(@NonNull Context context) {
    // Do not load ad if there is an unused ad or one is already loading.
    if (isLoadingAd || isAdAvailable()) {
      Log.d(Constant.TAG, "App open ad is either loading or has already loaded.");
      return;
    }

    isLoadingAd = true;
    AppOpenAd.load(
        new AdRequest.Builder(AppOpenFragment.AD_UNIT_ID).build(),
        new AdLoadCallback<AppOpenAd>() {
          @Override
          public void onAdLoaded(@NonNull AppOpenAd ad) {
            appOpenAd = ad;
            isLoadingAd = false;
            loadTime = new Date().getTime();
            Log.d(Constant.TAG, "App open ad loaded.");
          }

          @Override
          public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            isLoadingAd = false;
            Log.w(Constant.TAG, "App open ad failed to load: " + loadAdError);
          }
        });
  }

  // ...

  /** Check if ad was loaded more than n hours ago. */
  private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
    long dateDifference = new Date().getTime() - loadTime;
    long numMilliSecondsPerHour = 3600000L;
    return dateDifference < numMilliSecondsPerHour * numHours;
  }

  /** Check if ad exists and can be shown. */
  private boolean isAdAvailable() {
    // App open ads expire after four hours. Ads rendered more than four hours after request time
    // are no longer valid and may not earn revenue.
    return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
  }
}

Theo dõi hoạt động hiện tại

Để hiển thị quảng cáo, bạn sẽ cần một ngữ cảnh Activity. Để theo dõi hoạt động mới nhất đang được sử dụng, hãy đăng ký và triển khai Application.ActivityLifecycleCallbacks.

Kotlin

class MyApplication : Application(), Application.ActivityLifecycleCallbacks {

  private var currentActivity: Activity? = null

  override fun onCreate() {
    super<Application>.onCreate()
    registerActivityLifecycleCallbacks(this)
  }

  /** ActivityLifecycleCallback methods. */
  override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}

  override fun onActivityStarted(activity: Activity) {
    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) {}

  // ...
}

Java

public class MyApplication extends Application
  implements Application.ActivityLifecycleCallbacks {

  private Activity currentActivity;

  @Override
  public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(this);
  }

  /** ActivityLifecycleCallback methods. */
  @Override
  public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {}

  @Override
  public void onActivityStarted(@NonNull Activity activity) {
    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 bundle) {}

  @Override
  public void onActivityDestroyed(@NonNull Activity activity) {}

  // ...
}

registerActivityLifecycleCallbacks cho phép bạn theo dõi tất cả sự kiện Activity. Bằng cách theo dõi thời điểm bắt đầu và huỷ bỏ các hoạt động, bạn có thể theo dõi một tệp tham chiếu đến Activity hiện tại mà sau đó, bạn sẽ dùng để trình bày quảng cáo khi mở ứng dụng.

Theo dõi các sự kiện đưa ứng dụng lên nền trước

Để theo dõi các sự kiện đưa ứng dụng lên nền trước, hãy làm theo các bước sau:

Thêm các thư viện vào tệp gradle

Để được thông báo về các sự kiện đưa ứng dụng lên nền trước, bạn cần đăng ký một DefaultLifecycleObserver. Thêm phần phụ thuộc của nó vào tệp bản dựng cấp ứng dụng:

Kotlin

  dependencies {
    implementation("com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.21.0-beta01")
    implementation("androidx.lifecycle:lifecycle-process:2.8.3")
  }

Groovy

  dependencies {
    implementation 'com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.21.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-process:2.8.3'
  }

Triển khai giao diện đối tượng tiếp nhận dữ liệu vòng đời

Bạn có thể theo dõi các sự kiện đưa lên nền trước bằng cách triển khai giao diện DefaultLifecycleObserver.

Triển khai onStart() để hiển thị quảng cáo khi mở ứng dụng.

Kotlin

class MyApplication :
  Application(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {

  private var currentActivity: Activity? = null

  override fun onCreate() {
    super<Application>.onCreate()
    registerActivityLifecycleCallbacks(this)
    ProcessLifecycleOwner.get().lifecycle.addObserver(this)
  }

  /**
  * DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
  */
  override fun onStart(owner: LifecycleOwner) {
    currentActivity?.let { activity ->
      AppOpenAdManager.showAdIfAvailable(activity, null)
    }
  }

  // ...
}

Java

public class MyApplication extends Application
  implements Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {

  private Activity currentActivity;

  @Override
  public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(this);
    ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
  }

  /**
  * DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
  */
  @Override
  public void onStart(@NonNull LifecycleOwner owner) {
    if (currentActivity == null) {
      return;
    }

    AppOpenAdManager.getInstance().showAdIfAvailable(currentActivity, null);
  }

  // ...
}

Khởi động nguội và màn hình tải

Từ đầu đến giờ, tài liệu này giả định rằng bạn chỉ hiển thị quảng cáo khi mở ứng dụng vào lúc người dùng đưa ứng dụng của bạn lên nền trước khi ứng dụng bị tạm ngưng trong bộ nhớ. Quy trình "Khởi động nguội" xảy ra khi người dùng chạy ứng dụng của bạn nhưng trước đó, ứng dụng không bị tạm ngưng trong bộ nhớ.

Một ví dụ về khởi động nguội là khi người dùng mở ứng dụng của bạn lần đầu tiên. Trong trường hợp khởi động nguội, quảng cáo khi mở ứng dụng chưa được tải trước lần nào nên chưa sẵn sàng để hiển thị ngay lập tức. Độ trễ giữa thời điểm bạn yêu cầu quảng cáo và thời điểm nhận được quảng cáo có thể tạo ra tình huống người dùng có thể sử dụng ứng dụng của bạn trong thời gian ngắn trước khi bị bất ngờ bởi một quảng cáo không phù hợp. Bạn nên tránh làm như vậy vì điều này sẽ tạo ra trải nghiệm kém cho người dùng.

Để sử dụng quảng cáo khi mở ứng dụng vào lúc khởi động nguội, cách tốt nhất là bạn nên dùng màn hình tải để tải các thành phần trò chơi hoặc ứng dụng và chỉ nên hiển thị quảng cáo trên màn hình tải. Bạn đừng hiển thị quảng cáo nếu ứng dụng đã tải xong và đã đưa người dùng đến nội dung chính của ứng dụng.

Các phương pháp hay nhất

Quảng cáo khi mở ứng dụng giúp bạn kiếm tiền từ màn hình tải của ứng dụng, khi ứng dụng chạy lần đầu và khi chuyển đổi ứng dụng. Tuy nhiên, bạn cần ghi nhớ các phương pháp hay nhất để làm cho người dùng thích sử dụng ứng dụng của bạn. Tốt nhất là bạn nên:

  • Hiển thị quảng cáo khi mở ứng dụng đầu tiên sau khi người dùng đã sử dụng ứng dụng của bạn vài lần.
  • Hiển thị quảng cáo khi mở ứng dụng trong thời gian người dùng chờ ứng dụng của bạn tải.
  • Nếu bạn có màn hình tải trong quảng cáo khi mở ứng dụng và màn hình tải đó đã tải xong trước khi quảng cáo bị đóng, thì bạn nên đóng màn hình tải theo phương thức onAdDismissedFullScreenContent().

Ví dụ:

Tải xuống và chạy ứng dụng mẫu minh hoạ cách sử dụng SDK quảng cáo trên thiết bị di động của Google (bản thử nghiệm).