Esta guía está dirigida a los publicadores que integran anuncios de aplicación abierta con la SDK de Android para anuncios para dispositivos móviles.
Los anuncios de aplicación abierta son un formato de anuncio especial destinado a los publicadores que desean monetizar las pantallas de carga de la aplicación. Los anuncios de aplicación abierta se pueden cerrar en cualquier momento y están diseñados se muestre cuando los usuarios lleven la app al primer plano.
Los anuncios de aplicación abierta muestran automáticamente un área de desarrollo de la marca pequeña para que los usuarios sepan que están allí tu app. Este es un ejemplo de cómo se ve un anuncio de aplicación abierta:
A grandes rasgos, estos son los pasos importantes:
- Extiende la clase
Application
a fin de inicializar el SDK de anuncios de Google para dispositivos móviles. - Crea una clase de utilidad que cargue un anuncio antes de que tengas que mostrarlo.
- Carga un anuncio.
- Escuchar
ActivityLifecycleCallbacks.
- Muestra el anuncio y controla las devoluciones de llamada.
- Implementa y registra la interfaz de
LifecycleObserver
para mostrar un anuncio durante eventos en primer plano.
Requisitos previos
- SDK de anuncios de Google para dispositivos móviles versión 19.4.0 o posterior.
- Sigue las instrucciones de configuración que se encuentran en la página Cómo comenzar de la guía de YouTube.
Probar siempre con anuncios de prueba
Cuando compiles y pruebes tus apps, asegúrate de usar anuncios de prueba en lugar de anuncios activos y en producción. De lo contrario, podría suspenderse tu cuenta.
La forma más sencilla de cargar anuncios de prueba es usar nuestro ID de unidad de anuncios de prueba exclusivo para apps anuncios abiertos:
ca-app-pub-3940256099942544/9257395921
Se configuró de forma especial para mostrar anuncios de prueba para cada solicitud, y ahora usarla en tus propias apps mientras codificas, pruebas y depuras. Solo haz asegúrate de reemplazarla con tu propio ID de unidad de anuncios antes de publicar tu app.
Para obtener más información sobre cómo funcionan los anuncios de prueba del SDK de anuncios para dispositivos móviles, consulta el artículo Anuncios.
Extiende la clase Application
Crea una clase nueva que extienda la clase Application
y agrega lo siguiente:
para inicializar el SDK de anuncios de Google para dispositivos móviles.
Java
/** Application class that initializes, loads and show ads when activities change states. */
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();
}
}
Kotlin
/** Application class that initializes, loads and show ads when activities change states. */
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MyApplication) {}
}
}
}
Esto inicializará el SDK y proporcionará la estructura donde más adelante lo harás registrarse para los eventos en primer plano de la app.
A continuación, agrega el siguiente código a tu AndroidManifest.xml
:
<application
android:name="com.google.android.gms.example.appopendemo.MyApplication" ...>
...
</application>
Asegúrate de hacer referencia al nombre real de tu paquete.
Cómo implementar tu clase de utilidad
El anuncio se debe mostrar rápidamente. Por lo tanto, es mejor cargarlo antes de mostrarlo. De esta manera, tendrás un anuncio listo para publicarse en cuanto el usuario entre en tu aplicación. Implementa una clase de utilidad para realizar solicitudes de anuncios antes de que necesitan mostrar el anuncio.
Crea una clase nueva llamada AppOpenAdManager
en la clase MyApplication
.
complétalo de la siguiente manera:
Java
public class MyApplication extends Application {
...
/** Inner class that loads and shows app open ads. */
private class AppOpenAdManager {
private static final String LOG_TAG = "AppOpenAdManager";
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/9257395921";
private AppOpenAd appOpenAd = null;
private boolean isLoadingAd = false;
private boolean isShowingAd = false;
/** Constructor. */
public AppOpenAdManager() {}
/** Request an ad. */
private void loadAd(Context context) {
// We will implement this below.
}
/** Check if ad exists and can be shown. */
private boolean isAdAvailable() {
return appOpenAd != null;
}
}
}
Kotlin
private const val String LOG_TAG = "AppOpenAdManager"
private const val String AD_UNIT_ID = "ca-app-pub-3940256099942544/9257395921"
public class MyApplication extends Application {
...
/** Inner class that loads and shows app open ads. */
private inner class AppOpenAdManager {
private var appOpenAd: AppOpenAd? = null
private var isLoadingAd = false
var isShowingAd = false
/** Request an ad. */
fun loadAd(context: Context) {
// We will implement this below.
}
/** Check if ad exists and can be shown. */
private fun isAdAvailable(): Boolean {
return appOpenAd != null
}
}
}
Ahora que tienes una clase de utilidad, puedes crear una instancia en tu
Clase MyApplication
:
Java
public class MyApplication extends Application {
private AppOpenAdManager appOpenAdManager;
@Override
public void onCreate() {
super.onCreate();
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK 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 the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MyApplication) {}
}
appOpenAdManager = AppOpenAdManager()
}
}
Carga un anuncio
El siguiente paso es completar el método loadAd()
.
Java
private class AppOpenAdManager {
...
/** Request an ad. */
public void loadAd(Context context) {
// Do not load ad if there is an unused ad or one is already loading.
if (isLoadingAd || isAdAvailable()) {
return;
}
isLoadingAd = true;
AdRequest request = new AdRequest.Builder().build();
AppOpenAd.load(
context, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
new AppOpenAdLoadCallback() {
@Override
public void onAdLoaded(AppOpenAd ad) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "Ad was loaded.");
appOpenAd = ad;
isLoadingAd = false;
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, loadAdError.getMessage());
isLoadingAd = false;
}
});
}
...
}
Kotlin
private inner class AppOpenAdManager {
...
/** Request an ad. */
fun loadAd(context: Context) {
// Do not load ad if there is an unused ad or one is already loading.
if (isLoadingAd || isAdAvailable()) {
return
}
isLoadingAd = true
val request = AdRequest.Builder().build()
AppOpenAd.load(
context, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
object : AppOpenAdLoadCallback() {
override fun onAdLoaded(ad: AppOpenAd) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "Ad was loaded.")
appOpenAd = ad
isLoadingAd = false
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, loadAdError.message)
isLoadingAd = false;
}
})
}
...
}
AppOpenAdLoadCallback
tiene métodos a los que se llama cuando el elemento AppOpenAd
termine de cargarse.
Haz un seguimiento de la actividad actual
Para mostrar el anuncio, necesitarás un contexto de Activity
. Para hacer un seguimiento del
más reciente que use tu usuario, implementa el
Application.ActivityLifecycleCallbacks
en tu clase Application
.
Java
public class MyApplication extends Application implements ActivityLifecycleCallbacks {
private Activity currentActivity;
...
/** ActivityLifecycleCallback methods. */
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {
// Updating the currentActivity only when an ad is not showing.
if (!appOpenAdManager.isShowingAd) {
currentActivity = activity;
}
}
@Override
public void onActivityResumed(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivityPaused(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
Kotlin
class MyApplication : Application(), Application.ActivityLifecycleCallbacks {
private var currentActivity: Activity? = null
...
/** ActivityLifecycleCallback methods. */
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
override fun onActivityStarted(activity: Activity) {
// Updating the currentActivity only when an ad is not showing.
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) {}
}
Al realizar un seguimiento de la actividad actual, tienes un contexto para mostrar el
anuncio. Ahora debes registrar esta interfaz con el
registerActivityLifecycleCallbacks
.
Java
public class MyApplication extends Application {
...
@Override
public void onCreate() {
super.onCreate();
this.registerActivityLifecycleCallbacks(this);
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();
appOpenAdManager = new AppOpenAdManager();
}
}
Kotlin
class MyApplication : Application() {
...
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(this)
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MyApplication) {}
}
appOpenAdManager = AppOpenAdManager()
}
}
registerActivityLifecycleCallbacks
te permite escuchar todos los eventos de Activity
. Presta atención a las actividades
se inician y se destruyen, puedes llevar un registro de una referencia
Activity
, que luego usarás para presentar tu anuncio de aplicación abierta.
Muestra el anuncio y controla eventos de devolución de llamada en pantalla completa
El siguiente código demuestra cuándo mostrar y volver a cargar un anuncio.
Java
public class MyApplication extends Application {
...
/** Interface definition for a callback to be invoked when an app open ad is complete. */
public interface OnShowAdCompleteListener {
void onShowAdComplete();
}
private class AppOpenAdManager {
...
/** Shows the ad if one isn't already showing. */
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(LOG_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 (!isAdAvailable()) {
Log.d(LOG_TAG, "The app open ad is not ready yet.");
onShowAdCompleteListener.onShowAdComplete();
loadAd(activity);
return;
}
appOpenAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Set the reference to null so isAdAvailable() returns false.
Log.d(LOG_TAG, "Ad dismissed fullscreen content.");
appOpenAd = null;
isShowingAd = false;
onShowAdCompleteListener.onShowAdComplete();
loadAd(activity);
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Set the reference to null so isAdAvailable() returns false.
Log.d(LOG_TAG, adError.getMessage());
appOpenAd = null;
isShowingAd = false;
onShowAdCompleteListener.onShowAdComplete();
loadAd(activity);
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(LOG_TAG, "Ad showed fullscreen content.");
}
});
isShowingAd = true;
appOpenAd.show(activity);
}
...
}
}
Kotlin
class MyApplication : Application() {
...
/** Interface definition for a callback to be invoked when an app open ad is complete. */
interface OnShowAdCompleteListener {
fun onShowAdComplete()
}
private inner class AppOpenAdManager {
...
/** Shows the ad if one isn't already showing. */
fun showAdIfAvailable(
activity: Activity,
onShowAdCompleteListener: OnShowAdCompleteListener) {
// If the app open ad is already showing, do not show the ad again.
if (isShowingAd) {
Log.d(LOG_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 (!isAdAvailable()) {
Log.d(LOG_TAG, "The app open ad is not ready yet.")
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
return
}
appOpenAd?.setFullScreenContentCallback(
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Called when full screen content is dismissed.
// Set the reference to null so isAdAvailable() returns false.
Log.d(LOG_TAG, "Ad dismissed fullscreen content.")
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when fullscreen content failed to show.
// Set the reference to null so isAdAvailable() returns false.
Log.d(LOG_TAG, adError.message)
appOpenAd = null
isShowingAd = false
onShowAdCompleteListener.onShowAdComplete()
loadAd(activity)
}
override fun onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(LOG_TAG, "Ad showed fullscreen content.")
}
})
isShowingAd = true
appOpenAd?.show(activity)
}
...
}
}
FullScreenContentCallback
controla eventos como el momento en que se presenta el anuncio.
no presenta o cuando se descarta. Si un usuario regresa a tu app después
después de hacer clic en un anuncio
de aplicación abierta, se asegura de que no
se muestra otro anuncio de aplicación abierta.
Cómo escuchar eventos en primer plano de la app
Agrega las bibliotecas a tu archivo de Gradle
Para recibir notificaciones sobre los eventos que ocurren en primer plano de la app, debes registrar un
LifecycleObserver
Primero, edita el archivo build.gradle
a nivel de la aplicación para
Incluye las bibliotecas LifecycleObserver
:
apply plugin: 'com.android.application'
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.gms:play-services-ads:23.2.0'
def lifecycle_version = "2.3.1"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
}
Cómo implementar la interfaz de LifecycleObserver
Para escuchar eventos en primer plano en tu clase Application
, haz lo siguiente:
implementando la interfaz LifecycleObserver
.
Java
public class MyApplication extends Application
implements ActivityLifecycleCallbacks, LifecycleObserver { {
...
@Override
public void onCreate() {
super.onCreate();
this.registerActivityLifecycleCallbacks(this);
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
appOpenAdManager = new AppOpenAdManager();
}
/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
@OnLifecycleEvent(Event.ON_START)
protected void onMoveToForeground() {
// Show the ad (if available) when the app moves to foreground.
appOpenAdManager.showAdIfAvailable(currentActivity);
}
/** Show the ad if one isn't already showing. */
private void showAdIfAvailable(@NonNull final Activity activity) {
showAdIfAvailable(
activity,
new OnShowAdCompleteListener() {
@Override
public void onShowAdComplete() {
// Empty because the user will go back to the activity that shows the ad.
}
});
}
}
Kotlin
class MyApplication : Application(),
Application.ActivityLifecycleCallbacks, LifecycleObserver {
...
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(this)
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MyApplication) {}
}
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
appOpenAdManager = AppOpenAdManager()
}
/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onMoveToForeground() {
// Show the ad (if available) when the app moves to foreground.
currentActivity?.let {
appOpenAdManager.showAdIfAvailable(it)
}
}
/** Show the ad if one isn't already showing. */
fun showAdIfAvailable(activity: Activity) {
showAdIfAvailable(
activity,
object : OnShowAdCompleteListener {
override fun onShowAdComplete() {
// Empty because the user will go back to the activity that shows the ad.
}
})
}
}
Si registras tu LifecycleObserver
, tu app recibirá una alerta sobre el lanzamiento de la app
y eventos en primer plano, y mostrar el anuncio en los momentos adecuados.
Ten en cuenta el vencimiento de los anuncios
Para asegurarte de no mostrar un anuncio vencido, agrega un método a AppOpenAdManager
.
que verifica cuánto tiempo transcurrió desde que se cargó la referencia del anuncio. Luego, utiliza esa
para comprobar si el anuncio sigue siendo válido.
Java
private class AppOpenAdManager {
...
/** Keep track of the time an app open ad is loaded to ensure you don't show an expired ad. */
private long loadTime = 0;
/** Request an ad. */
public void loadAd(Context context) {
// Do not load ad if there is an unused ad or one is already loading.
if (isLoadingAd || isAdAvailable()) {
return;
}
isLoadingAd = true;
AdRequest request = new AdRequest.Builder().build();
AppOpenAd.load(
context, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
new AppOpenAdLoadCallback() {
@Override
public void onAdLoaded(AppOpenAd ad) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "Ad was loaded.");
appOpenAd = ad;
isLoadingAd = false;
loadTime = (new Date()).getTime();
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, loadAdError.getMessage());
isLoadingAd = false;
}
});
}
...
/** Utility method to check if ad was loaded more than n hours ago. */
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
long dateDifference = (new Date()).getTime() - this.loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * numHours));
}
/** Check if ad exists and can be shown. */
public boolean isAdAvailable() {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
}
}
Kotlin
private inner class AppOpenAdManager {
...
/** 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
/** Request an ad. */
fun loadAd(context: Context) {
// Do not load ad if there is an unused ad or one is already loading.
if (isLoadingAd || isAdAvailable()) {
return
}
isLoadingAd = true
val request = AdRequest.Builder().build()
AppOpenAd.load(
context, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
object : AppOpenAdLoadCallback() {
override fun onAdLoaded(ad: AppOpenAd) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "Ad was loaded.")
appOpenAd = ad
isLoadingAd = false
loadTime = Date().time
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, loadAdError.message)
isLoadingAd = false;
}
})
}
...
private fun wasLoadTimeLessThanNHoursAgo(numHours: Long): Boolean {
val dateDifference: Long = Date().time - loadTime
val numMilliSecondsPerHour: Long = 3600000
return dateDifference < numMilliSecondsPerHour * numHours
}
private fun isAdAvailable(): Boolean {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4)
}
}
Inicios en frío y pantallas de carga
Hasta ahora, en la documentación se supone que solo muestras anuncios de aplicación abierta cuando los usuarios primer plano de tu app cuando está suspendida en la memoria. "Inicios en frío" se producen cuando se inició tu app, pero no se suspendió anteriormente en la memoria.
Un ejemplo de inicio en frío es cuando un usuario abre tu app por primera vez. Con los inicios en frío, no tendrás un anuncio de aplicación abierta previamente cargado que esté listo para se mostrará de inmediato. La demora entre el momento en que solicitas un anuncio y lo recibes puede crear una situación en la que los usuarios puedan usar brevemente tu app se sorprenden con un anuncio fuera de contexto. Esto se debe evitar porque es un mala experiencia del usuario.
La forma preferida de usar los anuncios de aplicación abierta durante inicios en frío es mediante una pantalla de carga para cargar tus recursos de juego o app, y mostrar solo el anuncio en la pantalla. Si tu app terminó de cargarse y envió al usuario a la página principal contenido de tu app, no muestres el anuncio.
Prácticas recomendadas
Los anuncios de aplicación abierta te ayudan a monetizar la pantalla de carga de tu app cuando esta se encuentra por primera vez durante lanzamientos y cambios de apps, pero es importante mantener para que tus usuarios disfruten de usar tu app. Lo mejor es hacer lo siguiente:
- Muestre su primer anuncio de aplicación abierta después de que los usuarios hayan utilizado su aplicación varias veces.
- Muestra anuncios de aplicación abierta en los momentos en que los usuarios estarían esperando para que cargue tu app.
- Si hay una pantalla de carga debajo del anuncio de aplicación abierta
termine de cargarse antes de que se descarte el anuncio, quizás desee descartar sus
pantalla de carga en el método
onAdDismissedFullScreenContent()
Ejemplos en GitHub
Próximos pasos
Explora los siguientes temas: