Trang này so sánh cách triển khai quảng cáo gốc giữa SDK Quảng cáo của Google trên thiết bị di động (phiên bản thử nghiệm) và phiên bản hiện tại.
Tải một quảng cáo
Bảng sau đây so sánh quy trình triển khai chính khi tải một quảng cáo gốc:
Mục tiêu triển khai | Hiện tại | SDK Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
---|---|---|
Chỉ định các loại quảng cáo gốc | Thông qua các phương thức riêng biệt trên AdLoader . |
Trong yêu cầu quảng cáo. |
Xử lý lệnh gọi lại thành công khi tải quảng cáo | Mỗi loại quảng cáo gốc đều có một lệnh gọi lại riêng. | Một giao diện duy nhất NativeAdLoaderCallback xử lý thành công lượt tải quảng cáo cho tất cả các loại quảng cáo gốc. |
Xử lý lệnh gọi lại lỗi khi tải quảng cáo | Trong một trình nghe riêng. | Trong vòng NativeAdLoaderCallback . |
Tải quảng cáo gốc
Các ví dụ sau đây tải một quảng cáo gốc:
Hiện tại |
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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
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. } } ); |
Tải quảng cáo gốc tuỳ chỉnh
Các ví dụ sau đây tải một quảng cáo gốc tuỳ chỉnh:
Hiện tại |
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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
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. } } ); |
Đặt các lựa chọn quảng cáo gốc
Các ví dụ sau đây đặt các lựa chọn quảng cáo gốc:
Hiện tại |
Trong SDK quảng cáo trên thiết bị di động hiện tại, hãy đặt các lựa chọn quảng cáo gốc trên 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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
Trong SDK quảng cáo trên thiết bị di động của Google (bản thử nghiệm), hãy đặt các lựa chọn quảng cáo gốc trên 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(); |
Đặt lệnh gọi lại sự kiện quảng cáo gốc
Các ví dụ sau đây đặt lệnh gọi lại sự kiện quảng cáo gốc:
Hiện tại |
Bạn phải đăng ký các lệnh gọi lại sự kiện quảng cáo gốc trước khi tải quảng cáo. 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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
SDK quảng cáo trên thiết bị di động của Google (bản thử nghiệm) hỗ trợ việc đăng ký lệnh gọi lại sự kiện quảng cáo sau khi quảng cáo gốc được tải. 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. } }); } } ); |
Hiển thị quảng cáo
Phần này trình bày sự khác biệt khi hiển thị quảng cáo gốc:
Sử dụng tên gói SDK quảng cáo trên thiết bị di động của Google (thử nghiệm)
Khi tạo một NativeAdView
trong XML, hãy cập nhật tên gói:
Hiện tại |
<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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
<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> |
Đăng ký thành phần nội dung nghe nhìn bằng NativeAdView
Các ví dụ sau đây đăng ký thành phần nội dung nghe nhìn bằng NativeAdView
:
Hiện tại |
SDK quảng cáo trên thiết bị di động hiện tại dự kiến rằng chế độ xem nội dung nghe nhìn sẽ được đăng ký bằng chế độ xem quảng cáo gốc trước khi đăng ký quảng cáo gốc. Tuy nhiên, API không thực thi hành vi này. 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 Quảng cáo của Google trên thiết bị di động (thử nghiệm) |
SDK Quảng cáo của Google trên thiết bị di động (bản thử nghiệm) thực thi việc đăng ký chế độ xem nội dung nghe nhìn bằng chế độ xem quảng cáo gốc cùng lúc với quảng cáo gốc. 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); } |