📜  如何在Android中集成Facebook受众网络(FAN)原生广告?

📅  最后修改于: 2021-05-10 16:16:06             🧑  作者: Mango

为了从Android应用程序或游戏中赚钱,有很多方法,例如应用程序内购买,赞助,广告等等。但是,还有一种流行的从Android应用程序中赚钱的方法是集成第三方广告,例如称为Facebook Audience Network(FAN)的广告。 Facebook Audience Network旨在帮助从用户体验中获利。通过使用高价值格式,高质量的广告和创新的发布者工具,它有助于在保持人们参与度的同时发展业务。

为什么选择Facebook Audience Network?

  • Facebook Audience Network是Google Admob通过Android或IOS App获利的最佳选择之一。
  • 最低支出为$ 100
  • 多种广告格式
  • 最大填充率
  • 有效每千次展示费用高
  • 优质广告
  • 个性化广告

Facebook受众网络的格式

Facebook Audience Network主要提供五种类型的灵活,高性能格式

  • 原生:您精心设计以适合应用的广告
  • 非页内广告:全屏广告可吸引注意力并成为体验的一部分。
  • 标语:各种位置的传统格式。
  • 奖励视频:一种由用户启动的身临其境的视频广告,可奖励观看用户。
  • 可玩性:先买后买的广告体验,允许用户在安装前预览游戏。

在本文中,我们将Facebook Audience Network Native Ads集成到Android应用中。

原生广告:

原生广告广告用于为应用程序的广告构建定制的体验。原生广告的有效每千次展示费用(eCPM)相对于横幅广告要高,并且还会带来更高的点击率(点击率) ,从而从该应用中获得更多收益。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。

样本gif

方法

步骤1:建立新专案

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,尽管我们将使用Java语言来实现该项目,但请选择Java作为语言。

第2步:在进入编码部分之前,请先执行一些预任务

  • 转到应用程序> res>值> colors.xml文件,然后设置应用程序的颜色。
XML


  
    #0F9D58
    #0F9D58
    #05af9b
      


XML


  
    
    
  
        
        
  
        
  
            
            
  
            
            
  
        
  
        
        
  
    
      
    
    
  
    
  
        
  
            
            
  
            
            
  
        
  
        
        


XML


  
    
    
  
        
        
    
  
    
    


Java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdOptionsView;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.MediaView;
import com.facebook.ads.NativeAd;
import com.facebook.ads.NativeAdLayout;
import com.facebook.ads.NativeAdListener;
import java.util.ArrayList;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
    // creating NativeAdLayout object
    private NativeAdLayout nativeAdLayout;
    // creating  LinearLayout object
    private LinearLayout adView;
    // creating  NativeAd object
    private NativeAd nativeAd;
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // initialize the FACEBOOK Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // getting reference of button from activity_main.xml and setting  OnClickListener
        findViewById(R.id.showNativeAdBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // load Native Ad on button click
                loadNativeAd();
            }
        });
  
    }
  
    // loadinng native Ad
    private void loadNativeAd() {
  
        // initializing nativeAd object
        nativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
  
        // creating  NativeAdListener
        NativeAdListener nativeAdListener = new NativeAdListener() {
  
            @Override
            public void onMediaDownloaded(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onMediaDownloaded", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onError(Ad ad, AdError adError) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onAdLoaded(Ad ad) {
  
                // showing Toast message
                Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
  
                if (nativeAd == null || nativeAd != ad) {
                    return;
                }
  
                // Inflate Native Ad into Container
                inflateAd(nativeAd);
            }
  
            @Override
            public void onAdClicked(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onAdClicked", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onLoggingImpression(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onLoggingImpression", Toast.LENGTH_SHORT).show();
            }
        };
  
        // Load an ad
        nativeAd.loadAd(
                nativeAd.buildLoadAdConfig()
                        .withAdListener(nativeAdListener)
                        .build());
    }
  
    // inflating the Ad
    void inflateAd(NativeAd nativeAd) {
  
        // unregister the native Ad View
        nativeAd.unregisterView();
  
        // Add the Ad view into the ad container.
        nativeAdLayout = findViewById(R.id.native_ad_container);
  
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
  
        // Inflate the Ad view.
        adView = (LinearLayout) inflater.inflate(R.layout.fan_native_ad_layout, nativeAdLayout, false);
  
        // adding view
        nativeAdLayout.addView(adView);
  
        // Add the AdOptionsView
        LinearLayout adChoicesContainer = findViewById(R.id.ad_choices_container);
        AdOptionsView adOptionsView = new AdOptionsView(MainActivity.this, nativeAd, nativeAdLayout);
        adChoicesContainer.removeAllViews();
        adChoicesContainer.addView(adOptionsView, 0);
  
        // Create native UI using the ad metadata.
        MediaView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
        TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
        MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
        TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
        TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
        TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
        Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
  
        // Setting  the Text.
        nativeAdTitle.setText(nativeAd.getAdvertiserName());
        nativeAdBody.setText(nativeAd.getAdBodyText());
        nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
        nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
        nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
        sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
  
        // Create a list of clickable views
        List clickableViews = new ArrayList<>();
        clickableViews.add(nativeAdTitle);
        clickableViews.add(nativeAdCallToAction);
  
        // Register the Title and  button to listen for clicks.
        nativeAd.registerViewForInteraction(adView, nativeAdMedia, nativeAdIcon, clickableViews);
    }
}


  • 转到Gradle脚本> build.gradle(模块:应用程序)部分,导入以下依赖项,然后在上面的弹出窗口中单击“立即同步”。
  • 转到应用程序>清单> AndroidManifest.xml部分,并允许“ Internet权限”。

步骤3:设计UI

  • 创建一个新的布局,其中包含原生广告的布局。转到应用程序> res>布局>右键单击>新建>布局资源文件,并将文件命名为fan_native_ad_layout。
  • 以下是fan_native_ad_layout.xml文件的代码。在代码内部添加了注释,以更详细地了解代码。

XML格式



  
    
    
  
        
        
  
        
  
            
            
  
            
            
  
        
  
        
        
  
    
      
    
    
  
    
  
        
  
            
            
  
            
            
  
        
  
        
        
  • 以下是activity_main.xml文件的代码。在代码内部添加了注释,以更详细地了解代码。

XML格式



  
    
    
  
        
        
    
  
    
    

步骤4:使用MainActivity。 Java文件

  • 打开MainActivity。该类中有一个Java文件,首先,创建NativeAdLayout, LinearLayout NativeAd类的对象。
  • 现在,在onCreate()方法中,初始化Facebook Audience Network SDK。
  • onCreate()方法之外创建一个私有的void loadNativeAd ()方法并对其进行定义。
  • loadNativeAd()内部,将广告侦听器添加到本地广告并显示相对的Toast消息。
  • onAdLoaded()方法内部,调用我们稍后创建的inflateAd()
  • 现在,创建一个无效的inflateAd ,它将膨胀fan_native_ad_layout.xmlactivity_main.xml文件中的视图,并向用户显示广告。
  • 以下是MainActivity的完整代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdOptionsView;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.MediaView;
import com.facebook.ads.NativeAd;
import com.facebook.ads.NativeAdLayout;
import com.facebook.ads.NativeAdListener;
import java.util.ArrayList;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
    // creating NativeAdLayout object
    private NativeAdLayout nativeAdLayout;
    // creating  LinearLayout object
    private LinearLayout adView;
    // creating  NativeAd object
    private NativeAd nativeAd;
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // initialize the FACEBOOK Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // getting reference of button from activity_main.xml and setting  OnClickListener
        findViewById(R.id.showNativeAdBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // load Native Ad on button click
                loadNativeAd();
            }
        });
  
    }
  
    // loadinng native Ad
    private void loadNativeAd() {
  
        // initializing nativeAd object
        nativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
  
        // creating  NativeAdListener
        NativeAdListener nativeAdListener = new NativeAdListener() {
  
            @Override
            public void onMediaDownloaded(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onMediaDownloaded", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onError(Ad ad, AdError adError) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onAdLoaded(Ad ad) {
  
                // showing Toast message
                Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
  
                if (nativeAd == null || nativeAd != ad) {
                    return;
                }
  
                // Inflate Native Ad into Container
                inflateAd(nativeAd);
            }
  
            @Override
            public void onAdClicked(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onAdClicked", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onLoggingImpression(Ad ad) {
                // showing Toast message
                Toast.makeText(MainActivity.this, "onLoggingImpression", Toast.LENGTH_SHORT).show();
            }
        };
  
        // Load an ad
        nativeAd.loadAd(
                nativeAd.buildLoadAdConfig()
                        .withAdListener(nativeAdListener)
                        .build());
    }
  
    // inflating the Ad
    void inflateAd(NativeAd nativeAd) {
  
        // unregister the native Ad View
        nativeAd.unregisterView();
  
        // Add the Ad view into the ad container.
        nativeAdLayout = findViewById(R.id.native_ad_container);
  
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
  
        // Inflate the Ad view.
        adView = (LinearLayout) inflater.inflate(R.layout.fan_native_ad_layout, nativeAdLayout, false);
  
        // adding view
        nativeAdLayout.addView(adView);
  
        // Add the AdOptionsView
        LinearLayout adChoicesContainer = findViewById(R.id.ad_choices_container);
        AdOptionsView adOptionsView = new AdOptionsView(MainActivity.this, nativeAd, nativeAdLayout);
        adChoicesContainer.removeAllViews();
        adChoicesContainer.addView(adOptionsView, 0);
  
        // Create native UI using the ad metadata.
        MediaView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
        TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
        MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
        TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
        TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
        TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
        Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
  
        // Setting  the Text.
        nativeAdTitle.setText(nativeAd.getAdvertiserName());
        nativeAdBody.setText(nativeAd.getAdBodyText());
        nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
        nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
        nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
        sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
  
        // Create a list of clickable views
        List clickableViews = new ArrayList<>();
        clickableViews.add(nativeAdTitle);
        clickableViews.add(nativeAdCallToAction);
  
        // Register the Title and  button to listen for clicks.
        nativeAd.registerViewForInteraction(adView, nativeAdMedia, nativeAdIcon, clickableViews);
    }
}

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!