FaceBook广告文档地址:https://developers.facebook.com/docs/audience-network/guides/ad-formats/interstitial/android
打开文档里面类别是在这样:
包含横幅广告,插页广告,视频广告等等,但是前期的集成工作都是相同的,如下:(在集成前要创建应用,获取你要集成的类型广告的ID~,这个自己在Facebook后台创建获取就好)
1,配置gradle
项目级build.gradle:
repositories {
//添加
mavenCentral()
}
app级别build.gradle:
dependencies {
//facebook
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.facebook.android:audience-network-sdk:5.+'
}
2,初始化sdk
public class YourApplication extends Application {
...
@Override
public void onCreate() {
super.onCreate();
// Initialize the Audience Network SDK
AudienceNetworkAds.initialize(this);
}
...
}
这里讲我使用过的插页广告和横幅广告两种:
3,广告类型
插页广告:(我需求是点击按钮播放,在点击的时候才调show(),如果是需要加载完成就播放,直接在onAdLoaded()里面调show()就好)
private var interstitialAd: InterstitialAd? = null
private var mNeedShow: Boolean = false//使用全局变量,当点击按钮广告没有加载完成时设置为true,当加载完成,值为true时,直接调用show()
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //初始化插页广告 initInsertAd() //广告设置监听 interstitialAd?.setAdListener(this)//我是让类实现InterstitialAdListener,重写里面的方法 interstitialAd?.loadAd() }
private fun initInsertAd() {
if (interstitialAd != null) {
interstitialAd?.destroy()
interstitialAd = null
}
interstitialAd = InterstitialAd(OHProApplication.mContext, “YOUR_PLACEMENT_ID”)//YOUR_PLACEMENT_ID:广告id
}
//重写的方法
override fun onAdClicked(p0: Ad?) { OLogsProUtil.d("Interstitial ad clicked!") } //加载错误调用的,可以在此处获取错误码 override fun onError(p0: Ad?, adError: AdError) { OLogsProUtil.d("Ad failed to load: "+adError.getErrorMessage()) } //加载完成调用的 override fun onAdLoaded(ad: Ad?) { if (interstitialAd ==null) { return } if (mNeedShow){// dismissProLoading()//进度条消失 interstitialAd?.show()//插页广告播放调用的方法 } }
//广告开始播放调用的 override fun onInterstitialDisplayed(p0: Ad?) { OLogsProUtil.d("Interstitial ad displayed.") } //广告页面消失调用的 override fun onInterstitialDismissed(ad: Ad?) { OLogsProUtil.d("Interstitial ad dismissed.") } override fun onLoggingImpression(p0: Ad?) { OLogsProUtil.d("Interstitial ad impression logged!") }
//我的点击按钮播放的方法
override fun onClick(orn: View?) { when (orn?.id) { R.id.myfragment_bottomad_iv -> { if(interstitialAd!!.isAdLoaded){ interstitialAd!!.show() }else{ //如果没有加载完成则展示进度条 showProLoading()//我的进度条方法 mNeedShow = true } } }
---------------------------------分割线---------------------------------------
横幅广告:(根据文档发现,Facebook的横幅广告不像 谷歌广告可以直接选择adSize或者自定义尺寸,而是需要自定义布局,然后调用addview的形式添加)
横幅广告需要在展示的位置添加一个布局(这个布局一会用来盛放你自定义的布局):
<!--facebook横幅广告--> <com.facebook.ads.NativeAdLayout android:layout_marginLeft="18dp" android:layout_marginRight="18dp" android:id="@+id/native_banner_ad_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/getintegral_topview_white_bg_rl" />
然后再回到类里处理:
private var nativeAdLayout: NativeAdLayout? = null
private var nativeBannerAd: NativeBannerAd? = null
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
//获取到这个布局 nativeAdLayout = findViewById(R.id.native_banner_ad_container)//横幅广告 initBannerAd() nativeBannerAd?.setAdListener(this) nativeBannerAd?.loadAd() }
private fun initBannerAd() {
nativeBannerAd = NativeBannerAd(activity, “YOUR_PLACEMENT_ID”)//YOUR_PLACEMENT_ID:广告id
}
//这个方法是在广告加载完成的onAdLoaded()方法里面调用
private fun inflateAd(nativeBannerAd: NativeBannerAd) {
adView = LayoutInflater.from(activity).inflate(
R.layout.native_banner_ad_unit,//小条目的样式,下方
nativeAdLayout,
false
) as LinearLayout
if (!isAdViewAdded) {
isAdViewAdded = true
nativeAdLayout?.addView(adView)
}
nativeBannerAd.unregisterView()
// Add the AdChoices icon
val adChoicesContainer = adView?.findViewById<RelativeLayout>(R.id.ad_choices_container)
val adOptionsView = AdOptionsView(activity, nativeBannerAd, nativeAdLayout)
adChoicesContainer?.removeAllViews()
adChoicesContainer?.addView(adOptionsView, 0)
// Create native UI using the ad metadata.
val nativeAdTitle = adView?.findViewById<TextView>(R.id.native_ad_title)
val nativeAdSocialContext = adView?.findViewById<TextView>(R.id.native_ad_social_context)
val sponsoredLabel = adView?.findViewById<TextView>(R.id.native_ad_sponsored_label)
val nativeAdIconView = adView?.findViewById<AdIconView>(R.id.native_icon_view)
val nativeAdCallToAction = adView?.findViewById<Button>(R.id.native_ad_call_to_action)
// Set the Text.
nativeAdCallToAction?.text = nativeBannerAd.adCallToAction
if (nativeBannerAd.hasCallToAction()) {
nativeAdCallToAction?.visibility = View.VISIBLE
} else {
nativeAdCallToAction?.visibility = View.INVISIBLE
}
nativeAdTitle?.text = nativeBannerAd.advertiserName
nativeAdSocialContext?.text = nativeBannerAd.adSocialContext
sponsoredLabel?.text = nativeBannerAd.sponsoredTranslation
// Register the Title and CTA button to listen for clicks.
val clickableViews = ArrayList<View>()
if (nativeAdTitle != null && nativeAdCallToAction != null) {
clickableViews.add(nativeAdTitle!!)
clickableViews.add(nativeAdCallToAction!!)
nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews)
}
}
//定义布局native_banner_ad_unit,横幅广告的样式,这也是文当中的官方样式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RelativeLayout android:id="@+id/ad_choices_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2dp" /> <TextView android:id="@+id/native_ad_sponsored_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="end" android:lines="1" android:padding="2dp" android:textColor="@android:color/darker_gray" android:textSize="12sp" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <com.facebook.ads.AdIconView android:id="@+id/native_icon_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:paddingLeft="53dp" android:paddingRight="83dp"> <TextView android:id="@+id/native_ad_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/native_ad_social_context" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textSize="12sp" /> </LinearLayout> <Button android:id="@+id/native_ad_call_to_action" android:layout_width="80dp" android:layout_height="50dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:background="#4286F4" android:gravity="center" android:paddingLeft="3dp" android:paddingRight="3dp" android:textColor="@android:color/white" android:textSize="12sp" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
//要实现的方法 override fun onAdClicked(p0: Ad?) { OLogsProUtil.d("Native ad clicked!") } override fun onMediaDownloaded(p0: Ad?) { OLogsProUtil.d("Native ad finished downloading all assets") } override fun onError(p0: Ad?, adError: AdError) { OLogsProUtil.d("Ad failed to load: "+adError.getErrorMessage()) } override fun onAdLoaded(ad: Ad?) { if (nativeBannerAd == null) { return } if (nativeBannerAd == ad) { inflateAd(nativeBannerAd!!) } } override fun onLoggingImpression(p0: Ad?) { OLogsProUtil.d("Native ad impression logged!") }
测试的时候要在后台配置Facebook账号,邮箱,AAID。
AAID如何获取,下方连接:
https://mp.csdn.net/console/editor/html/105957303
没有配置测试账号的话,加载广告可能会报:1001,no Fill ~~
配置完后如果展示不出来,检查是否翻墙,检查facebook登陆的账号是否正确,或者可以重启手机试试。
效果如图:
横幅:
插页(点击按钮展示):