繁体   English   中英

如何预加载原生广告,android?

[英]How to preload native ads, android?

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

这是我的代码:

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>

在一个片段.xml 中,只是一个用于广告的 FrameView。

使用 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;
            }
        }
    }
    
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM