如何预加载原生广告,安卓



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

这是我的代码:

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。

使用java在谷歌文档中有一个方法提到使用adloader.is加载

所以在你的

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

}

最新更新