Esta página compara as implementações de anúncios nativos entre o SDK atual e o SDK dos anúncios para dispositivos móveis do Google (Beta).
Carregar um anúncio
A tabela a seguir compara a implementação principal ao carregar um anúncio nativo:
Meta de implementação | Atual | SDK dos anúncios para dispositivos móveis do Google (Beta) |
---|---|---|
Especificar tipos de anúncios nativos | Usando métodos exclusivos no AdLoader . |
Na solicitação de anúncio. |
Processar o callback de sucesso do carregamento de anúncios | Cada tipo de anúncio nativo tem um callback separado. | Uma única interface NativeAdLoaderCallback processa o sucesso do carregamento de anúncios
para todos os tipos de anúncios nativos. |
Processar o callback de falha no carregamento de anúncios | Em um listener separado. | Em NativeAdLoaderCallback . |
Carregar um anúncio nativo
Os exemplos a seguir carregam um anúncio nativo:
Atual |
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 dos anúncios para dispositivos móveis do Google (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. } } ); |
Carregar um anúncio nativo personalizado
Os exemplos a seguir carregam um anúncio nativo personalizado:
Atual |
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 dos anúncios para dispositivos móveis do Google (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. } } ); |
Definir as opções de anúncios nativos
Os exemplos a seguir definem as opções de anúncios nativos:
Atual |
No SDK dos anúncios para dispositivos móveis atual, defina as opções de anúncio nativo em
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 dos anúncios para dispositivos móveis do Google (Beta) |
No SDK dos anúncios para dispositivos móveis do Google (Beta), defina as opções de anúncio nativo em
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(); |
Definir callbacks de eventos de anúncios nativos
Os exemplos a seguir definem os callbacks de eventos de anúncios nativos:
Atual |
Os callbacks de eventos de anúncios nativos precisam ser registrados antes do carregamento do anúncio. 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 dos anúncios para dispositivos móveis do Google (Beta) |
O SDK dos Anúncios para Dispositivos Móveis do Google (beta) permite registrar callbacks de eventos de anúncio assim que o anúncio nativo é carregado. 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 um anúncio
Esta seção aborda as diferenças na exibição de um anúncio nativo:
Usar um nome de pacote do SDK dos anúncios para dispositivos móveis do Google (Beta)
Ao criar um NativeAdView
em XML, atualize o nome do pacote:
Atual |
<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 dos anúncios para dispositivos móveis do Google (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> |
Registre o recurso de conteúdo de mídia com NativeAdView
.
Os exemplos a seguir registram o recurso de conteúdo de mídia com NativeAdView
:
Atual |
O SDK dos anúncios para dispositivos móveis atual espera que a visualização de mídia seja registrada com a visualização de anúncio nativo antes do registro do anúncio nativo. No entanto, a API não impõe esse comportamento. 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 dos anúncios para dispositivos móveis do Google (Beta) |
O SDK dos Anúncios para Dispositivos Móveis do Google (beta) exige o registro da visualização de mídia com a visualização de anúncio nativo ao mesmo tempo que o anúncio 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); } |