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

📅  最后修改于: 2021-05-10 14:04:47             🧑  作者: 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 Banner广告集成到Android应用中。横幅广告是矩形图像或文字广告,在应用程序布局中占据很小的空间。横幅广告易于实施,并且不会影响用户界面并逐渐增加收入。

范横幅广告

方法

步骤1:建立专案

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

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

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


    #0F9D58
    #0F9D58
    #05af9b


activity_main.xml


  
    
    


MainActivity.java
package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.AudienceNetworkAds;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a objects of Button class
    Button fbBanner_50, fbBanner_90, fbBanner_250;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // link those objects with their respective id's
        // that we have given in activity_main.xml file
        fbBanner_50 = (Button)findViewById(R.id.banner_50);
        fbBanner_90 = (Button)findViewById(R.id.banner_90);
        fbBanner_250
            = (Button)findViewById(R.id.banner_250);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // click listener to show Banner_50 Ad
        fbBanner_50.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.BANNER_HEIGHT_50);
                }
            });
  
        // click listener to show Banner_90 Ad
        fbBanner_90.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.BANNER_HEIGHT_90);
                }
            });
  
        // click listener to show Banner_250 Ad
        fbBanner_250.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.RECTANGLE_HEIGHT_250);
                }
            });
    }
  
    private void showBanner(AdSize adSize)
    {
        // creating object of AdView
        AdView bannerAd;
  
        // initializing AdView Object
        // AdView Constructor Takes 3 Arguments
        // 1)Context
        // 2)Placement Id
        // 3)AdSize
        bannerAd = new AdView(
            this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID",
            adSize);
  
        // Creating and initializing LinearLayout which
        // contains the ads
        LinearLayout adLinearContainer
            = (LinearLayout)findViewById(
                R.id.fb_banner_ad_container);
  
        // removing the views inside linearLayout
        adLinearContainer.removeAllViewsInLayout();
  
        // adding ad to the linearLayoutContainer
        adLinearContainer.addView(bannerAd);
  
        // banner AdListener
        bannerAd.setAdListener(new AdListener() {
            @Override
            public void onError(Ad ad, AdError adError)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this, "onError",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onAdLoaded(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onAdLoaded",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onAdClicked(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onAdClicked",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onLoggingImpression(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onLoggingImpression",
                              Toast.LENGTH_SHORT)
                    .show();
            }
        });
  
        // loading Ad
        bannerAd.loadAd();
    }
}


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

步骤3:设计UI

activity_main.xml文件中,仅使用三个按钮。因此,只要用户单击该按钮,就会弹出所需的横幅广告。为了包含横幅广告,在XML文件内添加了LinearLayout。这是activity_main.xml文件的代码。

activity_main.xml



  
    
    

步骤4:使用MainActivity。 Java文件

  • 打开MainActivity。类中有Java文件,首先创建Button类的对象。
  • 现在,在onCreate()方法内部,将这些对象与activity_main.xml文件中给出的各自的ID链接。
  • 现在在onCreate()方法中,初始化Facebook Audience Network SDK
  • onCreate()方法之外创建一个私有的void showBanner ()方法并对其进行定义。
  • showBanner()方法将AdSize作为参数,以显示具有不同广告尺寸的横幅
  • 因此,下一步是当用户单击相应的横幅广告按钮时调用showBanner()方法。
  • 现在,在oncreate()方法中为所有三个按钮创建一个ClickListener并调用showBanner() 使用不同的AdSize。
  • 现在调用setAdListener() 横幅广告,以便用户知道广告的状态。要添加adListener,打开showBanner() 方法,并将以下代码添加到bannerAd.loadAd()之前
  • 并在AdListener Override方法内部显示广告消息,以便用户了解广告的状态。以下是MainActivity的完整代码。 Java文件。

主要活动。Java

package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.AudienceNetworkAds;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a objects of Button class
    Button fbBanner_50, fbBanner_90, fbBanner_250;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // link those objects with their respective id's
        // that we have given in activity_main.xml file
        fbBanner_50 = (Button)findViewById(R.id.banner_50);
        fbBanner_90 = (Button)findViewById(R.id.banner_90);
        fbBanner_250
            = (Button)findViewById(R.id.banner_250);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // click listener to show Banner_50 Ad
        fbBanner_50.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.BANNER_HEIGHT_50);
                }
            });
  
        // click listener to show Banner_90 Ad
        fbBanner_90.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.BANNER_HEIGHT_90);
                }
            });
  
        // click listener to show Banner_250 Ad
        fbBanner_250.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showBanner(AdSize.RECTANGLE_HEIGHT_250);
                }
            });
    }
  
    private void showBanner(AdSize adSize)
    {
        // creating object of AdView
        AdView bannerAd;
  
        // initializing AdView Object
        // AdView Constructor Takes 3 Arguments
        // 1)Context
        // 2)Placement Id
        // 3)AdSize
        bannerAd = new AdView(
            this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID",
            adSize);
  
        // Creating and initializing LinearLayout which
        // contains the ads
        LinearLayout adLinearContainer
            = (LinearLayout)findViewById(
                R.id.fb_banner_ad_container);
  
        // removing the views inside linearLayout
        adLinearContainer.removeAllViewsInLayout();
  
        // adding ad to the linearLayoutContainer
        adLinearContainer.addView(bannerAd);
  
        // banner AdListener
        bannerAd.setAdListener(new AdListener() {
            @Override
            public void onError(Ad ad, AdError adError)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this, "onError",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onAdLoaded(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onAdLoaded",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onAdClicked(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onAdClicked",
                              Toast.LENGTH_SHORT)
                    .show();
            }
  
            @Override public void onLoggingImpression(Ad ad)
            {
                // Showing a toast message
                Toast
                    .makeText(MainActivity.this,
                              "onLoggingImpression",
                              Toast.LENGTH_SHORT)
                    .show();
            }
        });
  
        // loading Ad
        bannerAd.loadAd();
    }
}

输出:在模拟器上运行