如何在 android 中实现 Facebook 广告,我将指导您如何使用数据绑定查看 Facebook 横幅广告、插页式广告、原生广告,因此如果您想使用 findviewbyid 手动执行,由您决定
1。 Facebook 横幅广告:
您的 gradle 文件中的第一个广告 facebook 受众网络依赖项:
implementation 'com.facebook.android:audience-network-sdk:6.2.0'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityFamousPlacesBinding binding = ActivityFamousPlacesBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
com.facebook.ads.AdView adView = new com.facebook.ads.AdView(FamousPlacesActivity.this, getString(R.string.fb_placement_banner), AdSize.BANNER_HEIGHT_50);
LinearLayout bannerContainer = findViewById(R.id.banner_container);
/// here is am getting the banner view by enabling databinding you can
/// dobygetting the view like
// LinearLayout banner_container= findViewById(R.id.banner_container);
binding.banner_container.addView(adView);
adView.loadAd(adView.buildLoadAdConfig().withAdListener(new com.facebook.ads.AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
}).build());
}
在同步依赖后,在 mainfest 文件的 application 标签下添加这个标签:
<activity android:name="com.facebook.ads.AudienceNetworkActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
对于 xml 代码这样写:
<LinearLayout
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" />
对于 java 类:
请记住,当您声明 Adview Import 应属于 Facebook 网络时:
对于 Facebook 插页式广告,
先声明这些变量:
///in concreate method laod the ads first
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_page);
// load the interstitial ads
loadfanads();
}
///Normally we show Interstitial on backpressed it up to you where you wants to show
/// on button click or on backpressed button of any activity
private InterstitialAd interstitialAd;
private void loadfanads() {
interstitialAd = new InterstitialAd(this, getString(R.string.fb_placement_Interstitial));
InterstitialAdListener madlistner = new InterstitialAdListener() {
@Override
public void onInterstitialDisplayed(Ad ad) {
}
@Override
public void onInterstitialDismissed(Ad ad) {
//// on Interstitial dismissed
Intent out = new Intent();
out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
setResult(RESULT_OK, out);
finish();
}
@Override
public void onError(Ad ad, AdError adError) {
/// on error ad loading
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
//// on ad clicked
Intent out = new Intent();
out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
setResult(RESULT_OK, out);
finish();
}
@Override
public void onLoggingImpression(Ad ad) {
}
};
interstitialAd.loadAd(interstitialAd.buildLoadAdConfig().withAdListener(madlistner).build());
}
要显示广告,你可以实现这个方法,我在后按后显示广告,但你可以选择显示:
//// on button click view
public void saveNow(View view) {
if (interstitialAd.isAdLoaded() && interstitialAd!=null && !interstitialAd.isAdInvalidated()){
interstitialAd.show();
}
else {
Intent out = new Intent();
out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
setResult(RESULT_OK, out);
finish();
}
}
对于 Facebook 原生横幅广告:
XML 代码只需添加此布局:
<RelativeLayout
android:id="@+id/templateContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
for jave code :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
nativeBannerAd = new NativeBannerAd(this, getString(R.string.fb_placement_native_banner));
NativeAdListener adlistner = new NativeAdListener() {
@Override
public void onMediaDownloaded(Ad ad) {
}
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
View adView = NativeBannerAdView.render(MainActivity.this, nativeBannerAd, NativeBannerAdView.Type.HEIGHT_120);
scarymainBinding.templateContainer.addView(adView);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
};
nativeBannerAd.loadAd(
nativeBannerAd.buildLoadAdConfig()
.withAdListener(adlistner)
.build());
}
如果您对此有任何疑问,可以问我!希望你会喜欢这个。