FaceBook广告文档地址:https://developers.facebook.com/docs/audience-network/guides/ad-formats/interstitial/android
打开文档里面类别是在这样:
包含横幅广告,插页广告,视频广告等等,但是前期的集成工作都是相同的,如下:(在集成前要创建应用,获取你要集成的类型广告的ID~,这个自己在Facebook后台创建获取就好)
项目级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.+'
}
public class YourApplication extends Application {
...
@Override
public void onCreate() {
super.onCreate();
// Initialize the Audience Network SDK
AudienceNetworkAds.initialize(this);
}
...
}
这里讲我使用过的插页广告和横幅广告两种:
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 } } }
横幅广告需要在展示的位置添加一个布局(这个布局一会用来盛放你自定义的布局):
然后再回到类里处理:
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(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 (R.id.native_ad_title) val nativeAdSocialContext = adView?.findViewById (R.id.native_ad_social_context) val sponsoredLabel = adView?.findViewById (R.id.native_ad_sponsored_label) val nativeAdIconView = adView?.findViewById (R.id.native_icon_view) val nativeAdCallToAction = adView?.findViewById
//定义布局native_banner_ad_unit,横幅广告的样式,这也是文当中的官方样式
//要实现的方法 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!") }
AAID如何获取,下方连接:
https://mp.csdn.net/console/editor/html/105957303
没有配置测试账号的话,加载广告可能会报:1001,no Fill ~~
配置完后如果展示不出来,检查是否,检查facebook登陆的账号是否正确,或者可以重启手机试试。
效果如图:
横幅:
插页(点击按钮展示):