📜  如何在 Android 中集成 Facebook Audience Network (FAN) 奖励视频广告?

📅  最后修改于: 2022-05-13 01:54:54.930000             🧑  作者: Mango

如何在 Android 中集成 Facebook Audience Network (FAN) 奖励视频广告?

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

为什么选择 Facebook 受众网络?

  • Facebook Audience Network 是 Google Admob 通过 Android 或 IOS 应用获利的最佳选择之一。
  • 最低支出为100 美元
  • 广泛的广告格式
  • 最大填充率
  • eCPM (每千次有效成本)
  • 优质广告
  • 个性化广告

Facebook Audience Network的格式

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

  • 原生:您为应用设计的广告,无缝衔接
  • 插页式广告:吸引注意力并成为体验一部分的全屏广告。
  • 横幅:各种展示位置的传统格式。
  • 奖励视频:用户发起的沉浸式视频广告,奖励用户观看。
  • Playables:一种先试后买的广告体验,允许用户在安装前预览游戏。

在本文中,让我们将Facebook Audience Network 奖励视频广告集成到 Android 应用程序中。

奖励视频:

  • 激励视频广告是覆盖应用整个 UI 的全屏广告。激励视频广告的eCPM (Effective Cost Per Mille)相对高于横幅广告插页式广告,并且还导致更高的 CTR (点击率),从而从应用中获得更多收入。
  • 当用户从头到尾观看奖励视频时,他们会获得应用内奖励。

粉丝重温视频

方法

第 1 步:创建项目

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

第 2 步:在进入编码部分之前先做一些前置任务

  • 转到app -> res -> values -> 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.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.RewardedVideoAd;
import com.facebook.ads.RewardedVideoAdListener;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a object of Button class
    Button showVideoAdBtn;
  
    @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
        showVideoAdBtn
            = (Button)findViewById(R.id.showVideoBtn);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // initializing and loading rewarded video ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating object of RewardedVideoAd
    private RewardedVideoAd fbRewardedVideoAd;
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes 2 Arguments
        // 1)Context
        // 2)Placement Id
        fbRewardedVideoAd = new RewardedVideoAd(
            this, "YOUR_PLACEMENT_ID");
  
        // RewardedVideoAd AdListener
        fbRewardedVideoAd.setAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onError(Ad ad, AdError error)
                {
                    // 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();
                }
  
                @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();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // loading Ad
        fbRewardedVideoAd.loadAd();
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded
        // or Not
        if (fbRewardedVideoAd.isAdLoaded()) {
            // showing Video Ad
            fbRewardedVideoAd.show();
        }
        else {
            // Loading Video Ad If it  is Not Loaded
            fbRewardedVideoAd.loadAd();
        }
    }
}


  • 转到Gradle Scripts -> build.gradle (Module: app)部分并导入以下依赖项,然后单击上面弹出窗口中的“立即同步”
  • 转到app -> manifests -> AndroidManifests.xml部分并允许“Internet Permission”。

第 3 步:设计用户界面

activity_main.xml文件中只添加一个Button ,因此每当用户点击 Button 时,奖励视频广告就会播放。

activity_main.xml



  
    
    

第 4 步:使用MainActivity。Java

  • 打开主活动。类里面有Java文件,首先创建Button类的对象。
  • 现在在onCreate()方法中,将这些对象与它们在 activity_main.xml 文件中给出的 ID 链接起来。
  • 现在在onCreate()方法中,初始化Facebook Audience Network SDK
  • MainActivity中创建RewardedVideoAd对象。 Java
  • 接下来在onCreate()方法之外创建一个私有的 void loadRewardedVideoAd( ) 方法并定义它。
  • 初始化 SDK 后调用oncreate ()方法中的 loadRewardedVideoAd()
  • 接下来在onCreate( ) 方法之外创建一个 void showRewardedVideoAd()方法,我们稍后会调用该方法来展示广告。
  • 接下来是在用户单击显示广告按钮时调用showRewardedVideoAd()方法。
  • 现在在oncreate()方法中为按钮创建一个ClickListener并调用showRewardedVideoAd()
  • 现在我们为 Rewarded Video Ad 添加RewardedVideoAdListener ,让用户知道广告的状态。
  • 要添加RewardedVideoAdListener打开loadRewardedVideoAd()方法并在fbRewardedVideoAd.loadAd() 之前添加以下代码;
  • 并在RewardedVideoAdListener Override 方法中显示一条 toast 消息,以便用户了解广告的状态。下面是MainActivity 的完整代码。 Java文件。

主要活动。Java

package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.RewardedVideoAd;
import com.facebook.ads.RewardedVideoAdListener;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a object of Button class
    Button showVideoAdBtn;
  
    @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
        showVideoAdBtn
            = (Button)findViewById(R.id.showVideoBtn);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // initializing and loading rewarded video ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating object of RewardedVideoAd
    private RewardedVideoAd fbRewardedVideoAd;
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes 2 Arguments
        // 1)Context
        // 2)Placement Id
        fbRewardedVideoAd = new RewardedVideoAd(
            this, "YOUR_PLACEMENT_ID");
  
        // RewardedVideoAd AdListener
        fbRewardedVideoAd.setAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onError(Ad ad, AdError error)
                {
                    // 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();
                }
  
                @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();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // loading Ad
        fbRewardedVideoAd.loadAd();
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded
        // or Not
        if (fbRewardedVideoAd.isAdLoaded()) {
            // showing Video Ad
            fbRewardedVideoAd.show();
        }
        else {
            // Loading Video Ad If it  is Not Loaded
            fbRewardedVideoAd.loadAd();
        }
    }
}

输出:在模拟器上运行