Rewarded Video Ads (Legacy API)

激励视频广告是一种全屏视频广告,用户可选择使用全屏模式观看,以换取应用内奖励。本指南介绍了如何将 Ad Manager 激励视频广告植入到 Android 应用中。

前提条件

初始化激励视频广告

Java

    package ...

    import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.reward.RewardedVideoAd;

    public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
        private RewardedVideoAd mRewardedVideoAd;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            // Use an activity context to get the rewarded video instance.
            mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
            mRewardedVideoAd.setRewardedVideoAdListener(this);
        }
        ...
    }

Kotlin

    package ...

    import com.google.android.gms.ads.doubleclick.PublisherAdRequest
    import com.google.android.gms.ads.MobileAds
    import com.google.android.gms.ads.reward.RewardedVideoAd

    class MainActivity : AppCompatActivity(), RewardedVideoAdListener {

    private lateinit var mRewardedVideoAd: RewardedVideoAd

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
            // Use an activity context to get the rewarded video instance.
            mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
            mRewardedVideoAd.rewardedVideoAdListener = this
        }
        ...
    }
    

可使用 MobileAds.getRewardedVideoAdInstance() 检索 RewardedVideoAd 对象。

要接收激励视频广告生命周期事件的通知,您必须实现 RewardedVideoAdListener。使用 setRewardedVideoAdListener() 方法可设置激励广告监听器。此监听器会自动从所有启用中介功能的广告联盟获得事件通知。例如,通过 RewardedVideoAdListener 上的 onRewarded() 方法,当某个用户因观看视频而获得奖励时,您就会收到通知。

请求激励视频广告

Java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        

        // Use an activity context to get the rewarded video instance.
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(this);

        loadRewardedVideoAd();
    }
    private void loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("/6499/example/rewarded-video",
                new PublisherAdRequest.Builder().build());
    }
    

Kotlin

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        

        // Use an activity context to get the rewarded video instance.
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
        mRewardedVideoAd.rewardedVideoAdListener = this

        loadRewardedVideoAd()
    }

    private fun loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("/6499/example/rewarded-video",
                PublisherAdRequest.Builder().build())
    }

根据 RewardedVideoAd 的单实例设计要求,应向共享实例提出加载广告的请求。

强烈建议您尽早调用 loadAd()(例如,在 Activity 的 onCreate() 方法中调用),以便预加载视频。

务必用测试广告进行测试

在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。

对于 Android 激励视频广告,加载测试广告最简便的方法就是使用下面的测试专用广告单元 ID:

/6499/example/rewarded-video

该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只是一定要在发布应用前用您自己的广告单元 ID 替换该测试广告单元 ID。

如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告

设置事件通知

在 SDK 提供的 RewardedVideoAdListener 接口中,存在与激励视频广告生命周期事件相对应的方法。为您的应用定义一个实现此接口的类,并在加载广告前将这个类传递给 setRewardedVideoAdListener

初始化激励视频广告部分显示的代码示例中,已介绍了如何就您的类实现 RewardedVideoAdListener 并在 RewardedVideoAd 对象上设置监听器进行声明。下面是监听器方法的实现示例:

Java

    @Override
    public void onRewarded(RewardItem reward) {
        Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
                reward.getAmount(), Toast.LENGTH_SHORT).show();
        // Reward the user.
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        Toast.makeText(this, "onRewardedVideoAdLeftApplication",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdClosed() {
        Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int errorCode) {
        Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdOpened() {
        Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoStarted() {
        Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoCompleted() {
        Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
    }
    

Kotlin

    override fun onRewarded(reward: RewardItem) {
        Toast.makeText(this, "onRewarded! currency: ${reward.type} amount: ${reward.amount}",
                Toast.LENGTH_SHORT).show()
        // Reward the user.
    }

    override fun onRewardedVideoAdLeftApplication() {
        Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoAdClosed() {
        Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoAdFailedToLoad(errorCode: Int) {
        Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoAdLoaded() {
        Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoAdOpened() {
        Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoStarted() {
        Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show()
    }

    override fun onRewardedVideoCompleted() {
        Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show()
    }
    

展示激励视频广告

Java

    if (mRewardedVideoAd.isLoaded()) {
        mRewardedVideoAd.show();
    }
    

Kotlin

    if (mRewardedVideoAd.isLoaded) {
        mRewardedVideoAd.show()
    }
    

我们建议您在尝试展示前确保激励视频广告已加载完毕。isLoaded() 方法可指示激励视频广告请求是否已成功完成。

使用 RewardedVideoAdListener 重新加载广告

在展示完一个激励视频广告后,您可以非常方便地使用 RewardedVideoAdListener 类的 onRewardedVideoAdClosed() 方法加载新的激励视频广告:

Java

    @Override
    public void onRewardedVideoAdClosed() {
        // Load the next rewarded video ad.
        loadRewardedVideoAd();
    }
    

Kotlin

    override fun onRewardedVideoAdClosed() {
        loadRewardedVideoAd()
    }
    

转发生命周期事件

要将父级 Activity 的生命周期事件转发给 RewardedVideoAd 对象,可在父级 Activity 的 onResume()onPause()onDestroy() 方法中分别调用 resume()pause()destroy() 方法。

下面是 Activity 生命周期事件的转发示例:

Java

    @Override
    public void onResume() {
        mRewardedVideoAd.resume(this);
        super.onResume();
    }

    @Override
    public void onPause() {
        mRewardedVideoAd.pause(this);
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mRewardedVideoAd.destroy(this);
        super.onDestroy();
    }
    

Kotlin

    override fun onPause() {
        super.onPause()
        mRewardedVideoAd.pause(this)
    }

    override fun onResume() {
        super.onResume()
        mRewardedVideoAd.resume(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        mRewardedVideoAd.destroy(this)
    }
    

与 Unity 集成

有关在 Unity 项目中集成激励视频广告的说明,请参阅激励视频广告

其他资源

GitHub 上的示例

后续步骤