Rewarded ads

前提条件

  • Google 移动广告 SDK 19.7.0 或更高版本。
  • 按照入门指南导入 Google 移动广告 SDK 并更新您的 Android 清单。

务必用测试广告进行测试

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

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

ca-app-pub-3940256099942544/5224354917

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

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

加载激励广告对象

针对 RewardedAd 类调用静态 load() 方法并传入 RewardedAdLoadCallback 可加载激励广告。这通常是在 ActivityonCreate() 方法中完成的。请注意,与其他广告格式加载回调一样,RewardedAdLoadCallback 会利用 LoadAdError 提供较高保真度的错误详情。

Java

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
  private RewardedAd mRewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    AdRequest adRequest = new AdRequest.Builder().build();

    RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
      adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // Handle the error.
          Log.d(TAG, loadAdError.getMessage());
          mRewardedAd = null;
        }

        @Override
        public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
          mRewardedAd = rewardedAd;
          Log.d(TAG, "Ad was loaded.");
        }
    });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private var mRewardedAd: RewardedAd? = null
  private final var TAG = 'MainActivity'

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

    var adRequest = AdRequest.Builder().build()

    RewardedAd.load(this,"ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.message)
        mRewardedAd = null
      }

      override fun onAdLoaded(rewardedAd: RewardedAd) {
        Log.d(TAG, "Ad was loaded.")
        mRewardedAd = rewardedAd
       }
    })
  }
}

设置 FullScreenContentCallback

FullScreenContentCallback 处理与展示 RewardedAd 相关的事件。在展示 RewardedAd 之前,请务必按如下方式设置回调:

Java

mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
  @Override
  public void onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad was shown.");
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // Called when ad fails to show.
    Log.d(TAG, "Ad failed to show.");
  }

  @Override
  public void onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad was dismissed.");
    mRewardedAd = null;
  }
});

Kotlin

mRewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad was shown.")
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    // Called when ad fails to show.
    Log.d(TAG, "Ad failed to show.")
  }

  override fun onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad was dismissed.")
    mRewardedAd = null
  }
}

展示广告

展示激励广告时,您将使用 OnUserEarnedRewardListener 对象处理奖励事件。

Java

if (mRewardedAd != null) {
  Activity activityContext = MainActivity.this;
  mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

Kotlin

if (mRewardedAd != null) {
  mRewardedAd?.show(this, OnUserEarnedRewardListener() {
    fun onUserEarnedReward(rewardItem: RewardItem) {
      var rewardAmount = rewardItem.getReward()
      var rewardType = rewardItem.getType()
      Log.d(TAG, "User earned the reward.")
    }
  })
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.")
}

常见问题解答

初始化调用是否会超时?
10 秒后,即使中介广告联盟仍未完成初始化,Google 移动广告 SDK 还是会调用 OnInitializationCompleteListener
在获得初始化回调时,如果某些中介广告联盟还没有准备好,该怎么办?

最佳做法是在 OnInitializationCompleteListener 的回调中加载广告。即使中介广告联盟尚未就绪,Google 移动广告 SDK 仍会向该广告联盟请求广告。因此,如果中介广告联盟在超时后完成初始化,它仍然可以在该会话中为将来的广告请求提供服务。

您可以通过调用 MobileAds.getInitializationStatus() 继续在整个应用会话中轮询所有适配器的初始化状态。

如何找出特定中介广告联盟未做好准备的原因?

AdapterStatus.getDescription() 描述了适配器未准备好为广告请求提供服务的原因。

GitHub 示例