En esta página, se proporciona una comparación de las implementaciones de anuncios nativos entre el SDK de anuncios de Google para dispositivos móviles (beta) y el actual.
Carga un anuncio
En la siguiente tabla, se comparan los aspectos clave de la implementación cuando se carga un anuncio nativo:
Objetivo de implementación | Actual | SDK de anuncios de Google para dispositivos móviles (beta) |
---|---|---|
Especifica los tipos de anuncios nativos | A través de métodos únicos en AdLoader . |
Dentro de la solicitud de anuncio |
Controla la devolución de llamada de carga exitosa del anuncio | Cada tipo de anuncio nativo tiene una devolución de llamada independiente. | Una sola interfaz NativeAdLoaderCallback controla el éxito de la carga de anuncios para todos los tipos de anuncios nativos. |
Controla la devolución de llamada de error en la carga de anuncios | En un objeto de escucha independiente | Dentro de NativeAdLoaderCallback |
Carga un anuncio nativo
En los siguientes ejemplos, se carga un anuncio nativo:
Actual |
Kotlinval adLoader = AdLoader.Builder(this, AD_UNIT_ID) .forNativeAd(object : NativeAd.OnNativeAdLoadedListener { override fun onNativeAdLoaded(nativeAd: NativeAd) { // Native ad loaded. } }) .withAdListener( object : AdListener() { override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Native ad failed to load. } } ) .build() adLoader.loadAd(AdRequest.Builder().build()) JavaAdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID) .forNativeAd(new NativeAd.OnNativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Native ad loaded. } }) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Native ad failed to load. } }) .build(); adLoader.loadAd(new AdRequest.Builder().build()); |
SDK de anuncios de Google para dispositivos móviles (beta) |
KotlinNativeAdLoader.load( NativeAdRequest.Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE)).build(), object : NativeAdLoaderCallback { override fun onNativeAdLoaded(nativeAd: NativeAd) { // Native ad loaded. } override fun onAdFailedToLoad(adError: LoadAdError) { // Native ad failed to load. } } ) JavaNativeAdLoader.load( new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE)).build(), new NativeAdLoaderCallback() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Native ad loaded. } @Override public void onAdFailedToLoad(LoadAdError adError) { // Native ad failed to load. } } ); |
Carga un anuncio nativo personalizado
En los siguientes ejemplos, se carga un anuncio nativo personalizado:
Actual |
Kotlinval adLoader = AdLoader.Builder(this, AD_UNIT_ID) .forCustomFormatAd(CUSTOM_FORMAT_ID, object: NativeCustomFormatAd.OnCustomFormatAdLoadedListener { override fun onCustomFormatAdLoaded(nativeCustomFormatAd: NativeCustomFormatAd) { // Custom native ad loaded. } }, object: NativeCustomFormatAd.OnCustomClickListener { override fun onCustomClick( nativeCustomFormatAd: NativeCustomFormatAd, assetName: String ) { // Custom native ad recorded a click. } }) .withAdListener( object : AdListener() { override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Custom native ad failed to load. } } ) .build() adLoader.loadAd(AdRequest.Builder().build()) JavaAdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID) .forCustomFormatAd(CUSTOM_FORMAT_ID, new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() { @Override public void onCustomFormatAdLoaded(NativeCustomFormatAd nativeCustomFormatAd) { // Custom native ad loaded. } }, new NativeCustomFormatAd.OnCustomClickListener() { @Override public void onCustomClick(NativeCustomFormatAd nativeCustomFormatAd, String assetName) { // Custom native ad recorded a click. } }) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Custom native ad failed to load. } }) .build(); adLoader.loadAd(new AdRequest.Builder().build()); |
SDK de anuncios de Google para dispositivos móviles (beta) |
KotlinNativeAdLoader.load( NativeAdRequest .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.CUSTOM_NATIVE)) .setCustomFormatIds(listOf(CUSTOM_FORMAT_ID)) .build(), object : NativeAdLoaderCallback { override fun onCustomNativeAdLoaded(customNativeAd: CustomNativeAd) { // Custom native ad loaded. } override fun onAdFailedToLoad(adError: LoadAdError) { // Custom native ad failed to load. } } ) JavaNativeAdLoader.load( new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAdType.CUSTOM_NATIVE)) .setCustomFormatIds(List.of(CUSTOM_FORMAT_ID)) .build(), new NativeAdLoaderCallback() { @Override public void onCustomNativeAdLoaded(CustomNativeAd customNativeAd) { // Custom native ad loaded. } @Override public void onAdFailedToLoad(LoadAdError adError) { // Custom native ad failed to load. } } ); |
Establece las opciones de anuncios nativos
En los siguientes ejemplos, se configuran las opciones del anuncio nativo:
Actual |
En el SDK de anuncios para dispositivos móviles actual, establece las opciones del anuncio nativo en Kotlinval videoOptions = VideoOptions.Builder().setStartMuted(true).build() val adLoader = AdLoader.Builder(this, AD_UNIT_ID) .withNativeAdOptions(NativeAdOptions.Builder().setVideoOptions(videoOptions).build()) .build() JavaVideoOptions videoOptions = new VideoOptions.Builder() .setStartMuted(true) .build(); AdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID) .withNativeAdOptions(new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build()) .build(); |
SDK de anuncios de Google para dispositivos móviles (beta) |
En el SDK de anuncios de Google para dispositivos móviles (beta), configura las opciones de anuncios nativos en Kotlinval videoOptions = VideoOptions.Builder().setStartMuted(true).build() val nativeAdRequest = NativeAdRequest .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE)) .setVideoOptions(videoOptions) .build() JavaVideoOptions videoOptions = new VideoOptions.Builder().setStartMuted(true).build(); NativeAdRequest nativeAdRequest = new NativeAdRequest .Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE)) .setVideoOptions(videoOptions) .build(); |
Configura devoluciones de llamada de eventos de anuncios nativos
En los siguientes ejemplos, se configuran las devoluciones de llamada de eventos de anuncios nativos:
Actual |
Las devoluciones de llamada de eventos de anuncios nativos se deben registrar antes de cargar el anuncio. Kotlinval adLoader = AdLoader.Builder(this, AD_UNIT_ID) .forNativeAd(object : NativeAd.OnNativeAdLoadedListener { override fun onNativeAdLoaded(nativeAd: NativeAd) { // Native ad loaded. } }) .withAdListener( object : AdListener() { override fun onAdOpened() { // Native ad opened an overlay that covered the screen. } override fun onAdClosed() { // Native ad closed. } override fun onAdImpression() { // Native ad recorded an impression. } override fun onAdClicked() { // Native ad recorded a click. } } ) .build() adLoader.loadAd(AdRequest.Builder().build()) JavaAdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID) .forNativeAd(new NativeAd.OnNativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Native ad loaded. } }) .withAdListener(new AdListener() { @Override public void onAdOpened() { // Native ad opened an overlay that covered the screen. } @Override public void onAdClosed() { // Native ad closed. } @Override public void onAdImpression() { // Native ad recorded an impression. } @Override public void onAdClicked() { // Native ad recorded a click. } }) .build(); adLoader.loadAd(new AdRequest.Builder().build()); |
SDK de anuncios de Google para dispositivos móviles (beta) |
El SDK de anuncios de Google para dispositivos móviles (beta) admite el registro de devoluciones de llamada de eventos de anuncios una vez que se carga el anuncio nativo. KotlinNativeAdLoader.load( NativeAdRequest .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE)) .build(), object : NativeAdLoaderCallback { override fun onNativeAdLoaded(nativeAd: NativeAd) { // Native ad loaded. nativeAd.adEventCallback = object : NativeAdEventCallback { override fun onAdShowedFullScreenContent() { // Native ad showed full screen content. // Current SDK equivalent: onAdOpened() } override fun onAdDismissedFullScreenContent() { // Native ad dismissed full screen content. // Current SDK equivalent: onAdClosed() } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Native ad failed to show full screen content. // Current SDK equivalent: N/A } override fun onAdImpression() { // Native ad recorded an impression. } override fun onAdClicked() { // Native ad recorded a click. } } } } ) JavaNativeAdLoader.load( new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE)) .build(), new NativeAdLoaderCallback() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Native ad loaded. nativeAd.setAdEventCallback(new NativeAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Native ad showed full screen content. // Current SDK equivalent: onAdOpened() } @Override public void onAdDismissedFullScreenContent() { // Native ad dismissed full screen content. // Current SDK equivalent: onAdClosed() } @Override public void onAdFailedToShowFullScreenContent(FullScreenContentError fullScreenContentError) { // Native ad failed to show full screen content. // Current SDK equivalent: N/A } @Override public void onAdImpression() { // Native ad recorded an impression. } @Override public void onAdClicked() { // Native ad recorded a click. } }); } } ); |
Mostrar un anuncio
En esta sección, se explican las diferencias en la publicación de un anuncio nativo:
Usar un nombre de paquete del SDK de anuncios de Google para dispositivos móviles (beta)
Cuando crees un NativeAdView
en XML, actualiza el nombre del paquete:
Actual |
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Layout assets such as the media view and call to action. --> </com.google.android.gms.ads.nativead.NativeAdView> |
SDK de anuncios de Google para dispositivos móviles (beta) |
<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Layout assets such as the media view and call to action. --> </com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView> |
Registra el recurso de contenido multimedia con NativeAdView
En los siguientes ejemplos, se registra el recurso de contenido multimedia con NativeAdView
:
Actual |
El SDK de anuncios para dispositivos móviles actual espera que la vista de medios se registre con la vista del anuncio nativo antes de registrar el anuncio nativo. Sin embargo, la API no aplica este comportamiento. Kotlinprivate fun displayNativeAd(nativeAd: NativeAd) { // Inflate the NativeAdView layout. val nativeAdBinding = NativeAdBinding.inflate(layoutInflater) // Add the NativeAdView to the view hierarchy. binding.nativeViewContainer.addView(nativeAdBinding.root) val nativeAdView = nativeAdBinding.root // Populate and register the asset views. nativeAdView.mediaView = nativeAdBinding.adMedia // ... // Register the native ad with the NativeAdView. nativeAdView.setNativeAd(nativeAd) } Javaprivate void displayNativeAd(NativeAd nativeAd) { // Inflate the NativeAdView layout NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater()); // Add the NativeAdView to the view hierarchy binding.nativeViewContainer.addView(nativeAdBinding.getRoot()); NativeAdView nativeAdView = nativeAdBinding.getRoot(); // Populate and register the asset views nativeAdView.setMediaView(nativeAdBinding.adMedia); // ... // Register the native ad with the NativeAdView nativeAdView.setNativeAd(nativeAd); } |
SDK de anuncios de Google para dispositivos móviles (beta) |
El SDK de anuncios de Google para dispositivos móviles (beta) exige el registro de la vista de medios con la vista del anuncio nativo al mismo tiempo que el anuncio nativo. Kotlinprivate fun displayNativeAd(nativeAd: NativeAd) { // Inflate the NativeAdView layout. val nativeAdBinding = NativeAdBinding.inflate(layoutInflater) // Add the NativeAdView to the view hierarchy. binding.nativeViewContainer.addView(nativeAdBinding.root) val nativeAdView = nativeAdBinding.root // Populate and register the asset views. // ... // Register the native ad and media content asset with the NativeAdView. val mediaView = nativeAdBinding.adMedia nativeAdView.registerNativeAd(nativeAd, mediaView) } Javaprivate void displayNativeAd(NativeAd nativeAd) { // Inflate the NativeAdView layout. NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater()); // Add the NativeAdView to the view hierarchy. binding.nativeViewContainer.addView(nativeAdBinding.getRoot()); NativeAdView nativeAdView = nativeAdBinding.getRoot(); // Populate and register the asset views. // ... // Register the native ad and media content asset with the NativeAdView. MediaView mediaView = nativeAdBinding.adMedia; nativeAdView.registerNativeAd(nativeAd, mediaView); } |