Rewarded Video Ads

激励视频广告是一种全屏视频广告,用户可以选择使用全屏模式观看,这样可以换取应用内奖励。本指南向您介绍如何将AdMob激励视频广告集成到 Android 应用中。

前提条件

初始化激励视频广告

Java

package ...

import com.google.android.gms.ads.AdRequest;
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);
        MobileAds.initialize(this, "ca-app-pub-3940256099942544/5224354917");

        // 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.AdRequest
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)
        MobileAds.initialize(this, "ca-app-pub-3940256099942544/5224354917")

        // 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);
    MobileAds.initialize(this, "ca-app-pub-3940256099942544/5224354917");

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

    loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().build());
}

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    MobileAds.initialize(this, "ca-app-pub-3940256099942544/5224354917")

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

    loadRewardedVideoAd()
}

private fun loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            AdRequest.Builder().build())
}

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

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

应始终使用测试广告进行测试

在开发和测试应用时,请确保使用测试广告而不是已发布的广告。否则,可能会导致您的帐号被暂停。

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

ca-app-pub-3940256099942544/5224354917

该测试广告单元 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 上的示例

视频教程

代码实验室

后续步骤

发送以下问题的反馈:

此网页
Google AdMob > Mobile Ads SDK > Android
Google AdMob > Mobile Ads SDK > Android
需要帮助?请访问我们的支持页面