【问题标题】:How to preload native ads, android?如何预加载原生广告,android?
【发布时间】:2020-07-18 20:56:39
【问题描述】:

我在我的应用程序中添加了一个原生广告,但在不同的设备上,广告,尤其是视频,会出现延迟,在模拟器上根本不会显示视频。我知道你可以以某种方式预加载广告, 但是我不知道怎么做,请给我解释一下。

这是我的代码:

class Fragment(private var index: Int, private var adsId: String) : Fragment() {

    var currentNativeAd: UnifiedNativeAd? = null
    lateinit var adLoader: AdLoader

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment, container, false)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val unifiedNativeAdView = layoutInflater.inflate(R.layout.native_ad_layout, null) as UnifiedNativeAdView

        val nativeAdLayout = view!!.findViewById<FrameLayout>(R.id.id_native_ad)

        val adLoader = AdLoader.Builder(activity, adsId).forUnifiedNativeAd { unifiedNativeAd ->
(unifiedNativeAd)
            mapUnifiedNativeAdToLayout(unifiedNativeAd, unifiedNativeAdView)
            nativeAdLayout.removeAllViews()

        }.withAdListener(object : AdListener() {
            // Code to be executed when an ad request fails.
            override fun onAdLoaded() {
                super.onAdLoaded()
                if (!adLoader.isLoading) {
                  nativeAdLayout.addView(unifiedNativeAdView)
                }
            }

            override fun onAdFailedToLoad(errorCode: Int) {
               Toast.makeText(applicationContext, "Ad failed to load! error code: $errorCode", Toast.LENGTH_SHORT).show()
            }

            override fun onAdClosed() {
                nativeAdLayout.removeAllViews()

            }

        }).build()
        adLoader.loadAd(AdRequest.Builder().build())
   
    }

    fun mapUnifiedNativeAdToLayout(adFromGoogle: UnifiedNativeAd, myAdView: UnifiedNativeAdView) {
        currentNativeAd?.destroy()
        currentNativeAd = adFromGoogle

        val mediaView: MediaView = myAdView.findViewById(R.id.ad_media)
        myAdView.mediaView = mediaView
        myAdView.bodyView = myAdView.findViewById(R.id.ad_body)

        if (adFromGoogle.body == null) {
            myAdView.bodyView.visibility = View.GONE
        } else {
            (myAdView.bodyView as TextView).text = adFromGoogle.body
        }

        myAdView.setNativeAd(adFromGoogle)

        val vc = adFromGoogle.videoController

        if (vc.hasVideoContent()) {

            vc.videoLifecycleCallbacks = object : VideoController.VideoLifecycleCallbacks() {
                override fun onVideoEnd() {
                    super.onVideoEnd()
                }
            }
        } 
    }

    override fun onDestroy() {
        currentNativeAd?.destroy()
        super.onDestroy()

    }
}

native_ad_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.formats.UnifiedNativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2"
                android:orientation="vertical">

                <com.google.android.gms.ads.formats.MediaView
                    android:id="@+id/ad_media"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:padding="@dimen/_8sdp"
                    android:layout_gravity="center"
                    android:layout_weight="2"
                    android:background="#fff" />

                <TextView
                    android:id="@+id/ad_body"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/sf_pro_display_regular"
                    android:gravity="center"
                    android:text="body of ad"
                    android:textSize="14sp" />

            </LinearLayout>

        </RelativeLayout>
    
    </LinearLayout>

</com.google.android.gms.ads.formats.UnifiedNativeAdView>

在 fragment.xml 中,只是广告的 FrameView。

【问题讨论】:

    标签: android kotlin admob native-ads google-ads-api


    【解决方案1】:

    使用 java 的谷歌文档中提到了使用 adloader.isLoading 的方法

    所以在你的

    override fun onAdLoaded() {
                    super.onAdLoaded()
    
                }
    
    you would ad if(!adLoader.isLoading){
        //insert ads to your recycler.
    }
    

    所以..

        override fun onAdLoaded() {
                        super.onAdLoaded()
                       if(!adLoader.isLoading){
    
        int number_of_posts_before_showing_ad = 5
    
      if (nativeAds.size() <= 0 || yourPostList.size() < number_of_posts_before_showing_ad) {
                return;
            } else {
                int offset = (yourPostList.size() / nativeAds.size() + 1);
                int index = 0;
                for (UnifiedNativeAd ad : nativeAds) {
                    yourPostList.add(index, ad);
                    recyclerViewAdapter.notifyDataSetChanged();
                    index += offset;
                }
            }
        }
        
                    }
    

    【讨论】:

    • 我只有一个声明,是的,我明白为什么需要这个方法,但我不明白它到底需要写什么。
    • 我添加了我从谷歌文档中使用的代码,这些代码显示了如何加载广告。基本上 if 语句说如果没有广告,并且帖子数量至少为 5,则加载广告。我假设的 for 循环遍历可用广告,偏移量和索引来自谷歌,用于在特定时间间隔在帖子之间放置广告
    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多