📜  如何在Android中集成Google Admob奖励视频广告?

📅  最后修改于: 2021-05-09 16:25:58             🧑  作者: Mango

为了从Android应用程序或游戏中赚钱,有很多方法,例如应用程序内购买,赞助,广告等等。但是,还有一种流行的从Android应用程序中赚钱的方法是集成广告,例如称为Google AdMob的广告。 Google AdMob的设计着眼于开发人员,AdMob可以通过自动工具帮助您赚取更多应用收入,提供更好的用户体验以及切实可行的见解,从而为您提供辛劳的服务。

为什么选择Google Admob广告网络?

  • 最低支出为$ 100
  • 多种广告格式
  • 最大填充率
  • 有效每千次展示费用高
  • 优质广告
  • 个性化广告

Google AdMob的格式

Google AdMob主要提供四种类型的灵活,高性能格式

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

在本文中,我们将集成Google Admob SDK以在Android应用中显示“奖励视频广告”。

奖励视频:

  •  奖励视频广告是覆盖应用程序整个用户界面的全屏广告。奖励视频广告的有效每千次展示费用( eCPM )相对于横幅广告和插页式广告而言相对较高,并且还会带来更高的点击率(点击率),从而从您的应用中获得更多收益。
  • 当用户从头到尾观看“奖励视频”时,都会获得应用内奖励。这种类型的广告主要用于游戏中,也可以在应用中使用。

方法

步骤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.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.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 Google Admob  SDK
        MobileAds.initialize(this);
  
        // loading Video Ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    // showing Ad
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating RewardedVideoAd object
    private RewardedVideoAd AdMobrewardedVideoAd;
  
    // AdMob Rewarded Video Ad Id
    private String AdId
        = "ca-app-pub-3940256099942544/5224354917";
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes Context as its
        // Argument
        AdMobrewardedVideoAd
            = MobileAds.getRewardedVideoAdInstance(this);
  
        // Rewarded Video Ad Listener
        AdMobrewardedVideoAd.setRewardedVideoAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onRewardedVideoAdLoaded()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdLoaded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdOpened()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdOpened",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoStarted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoStarted",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewarded(
                    RewardItem rewardItem)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewarded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void
                onRewardedVideoAdLeftApplication()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdLeftApplication",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdFailedToLoad(
                    int i)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdFailedToLoad",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // Loading Rewarded Video Ad
        AdMobrewardedVideoAd.loadAd(
            AdId, new AdRequest.Builder().build());
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded or Not
        if (AdMobrewardedVideoAd.isLoaded()) {
            // showing Video Ad
            AdMobrewardedVideoAd.show();
        }
        else {
            // Loading Rewarded Video Ad
            AdMobrewardedVideoAd.loadAd(
                AdId, new AdRequest.Builder().build());
        }
    }
}


AndroidManifest.xml


  
    
  
    
        
            
                
  
                
            
        
          
        
        
  
    
  


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

步骤3:设计UI

activity_main.xml中添加一个按钮,以便当用户单击该按钮时,奖励视频将在屏幕上播放。

activity_main.xml



  
    
    

步骤4:使用MainActivity。 Java文件

  • 打开MainActivity。该类内有Java文件,首先,创建Button类的对象。
  • 现在,在onCreate()方法内部,将这些对象与其在activity_main.xml文件中给出的各自的ID链接起来。
  • 现在在onCreate()方法中,初始化Google Admob SDK
  • 创建RewardedVideoAdMainActivity的对象。 Java
  • MainActivity中创建AdMob奖励视频广告ID 。 Java
  • 接下来,在onCreate()方法之外创建一个私有的void loadRewardedVideoAd ()方法并对其进行定义。
  • 初始化SDK后,请在oncreate()方法内调用loadRewardedVideoAd()
  • 接下来,在onCreate ()方法之外创建一个无效的showRewardedVideoAd()方法,稍后我们将其调用以展示广告。
  • 因此,下一步是调用showRewardedVideoAd()方法。现在,在oncreate()方法中,为按钮创建一个ClickListener并调用showRewardedVideoAd()。
  • 现在,为Rewarded Video Ad添加RewardedVideoAdListener ,以便用户知道广告的状态。
  • 要添加RewardedVideoAdListener开放loadRewardedVideoAd()方法和AdMobrewardedVideoAd.loadAd前添加以下代码(ADID,新AdRequest.Builder()的构建());
  • 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.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.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 Google Admob  SDK
        MobileAds.initialize(this);
  
        // loading Video Ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    // showing Ad
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating RewardedVideoAd object
    private RewardedVideoAd AdMobrewardedVideoAd;
  
    // AdMob Rewarded Video Ad Id
    private String AdId
        = "ca-app-pub-3940256099942544/5224354917";
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes Context as its
        // Argument
        AdMobrewardedVideoAd
            = MobileAds.getRewardedVideoAdInstance(this);
  
        // Rewarded Video Ad Listener
        AdMobrewardedVideoAd.setRewardedVideoAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onRewardedVideoAdLoaded()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdLoaded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdOpened()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdOpened",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoStarted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoStarted",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewarded(
                    RewardItem rewardItem)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewarded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void
                onRewardedVideoAdLeftApplication()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdLeftApplication",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdFailedToLoad(
                    int i)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdFailedToLoad",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // Loading Rewarded Video Ad
        AdMobrewardedVideoAd.loadAd(
            AdId, new AdRequest.Builder().build());
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded or Not
        if (AdMobrewardedVideoAd.isLoaded()) {
            // showing Video Ad
            AdMobrewardedVideoAd.show();
        }
        else {
            // Loading Rewarded Video Ad
            AdMobrewardedVideoAd.loadAd(
                AdId, new AdRequest.Builder().build());
        }
    }
}

步骤5:AndroidManifest.xml文件

以下是AndroidManifest.xml文件的完整代码。

AndroidManifest.xml



  
    
  
    
        
            
                
  
                
            
        
          
        
        
  
    
  

输出:在模拟器上运行