插页式激励广告是一种 一种利诱性质的广告格式,可让您为每次展示 自动应用。与激励广告不同,用户 用户才能选择观看插页式激励广告。
前提条件
* Google 移动广告 SDK 19.2.0 或更高版本。- 完成入门指南。
实现
植入插页式激励广告的主要步骤如下所示:
- 加载广告
- 注册全屏事件回调
- 处理奖励回调
- 展示广告
加载广告
广告的加载是通过对 RewardedInterstitialAd
类使用静态 load()
方法完成的。加载方法需要上下文,您的广告
一个单元 ID、一个 AdManagerAdRequest
对象和一个
RewardedInterstitialAdLoadCallback
,在广告加载成功时收到通知;或
失败。已加载的 RewardedInterstitialAd
对象会作为形参在
onRewardedInterstitialAdLoaded()
回调。
以下示例展示了如何在 RewardedInterstitialAd
MainActivity
。
Java
public class MainActivity extends AppCompatActivity {
private RewardedInterstitialAd rewardedInterstitialAd;
private String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
// Load an ad on the main thread.
runOnUiThread(
() -> {
loadAd();
});
})
.start();
}
public void loadAd() {
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded-interstitial",
new AdManagerAdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedInterstitialAd = ad;
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
}
Kotlin
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback
class MainActivity : AppCompactActivity() {
private var rewardedInterstitialAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MainActivity) {}
// Load an ad on the main thread.
runOnUiThread {
loadAd()
}
}
}
private fun loadAd() {
RewardedInterstitialAd.load(this, "/21775744923/example/rewarded-interstitial",
AdManagerAdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
}
注册回调
如需接收有关展示事件的通知,您必须向广告的 setter 方法传递 FullScreenContentCallback
对象。通过
FullScreenContentCallback
对象处理广告展示时的回调
以及何时关闭以下代码
展示了如何在 FullScreenContentCallback
RewardedInterstitialAdLoadCallback
:
Java
public void loadAd(){
RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded-interstitial",
new AdManagerAdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
rewardedInterstitialAd = ad;
rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@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 dismissed fullscreen content.");
rewardedInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedInterstitialAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
Kotlin
private fun loadAd() {
RewardedInterstitialAd.load(this, "/21775744923/example/rewarded-interstitial",
AdManagerAdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
rewardedInterstitialAd = ad
rewardedInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
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 dismissed fullscreen content.")
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
rewardedInterstitialAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
处理奖励
要展示您的插页式激励广告,请实现
OnUserEarnedRewardListener
界面 MainActivity
接收通知
当用户获得奖励时触发。
Java
public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
...
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "User earned reward.");
// TODO: Reward the user!
}
}
Kotlin
class MainActivity : AppCompatActivity(), OnUserEarnedRewardListener {
...
override fun onUserEarnedReward(rewardItem: RewardItem) {
Log.d(TAG, "User earned reward.")
// TODO: Reward the user!
}
}
展示广告
实现 OnUserEarnedRewardListener
接口后,
使用广告的 show()
方法,如下所示:
Java
rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
OnUserEarnedRewardListener */ MainActivity.this);
Kotlin
rewardedInterstitialAd?.show(/* Activity */ this, /*
OnUserEarnedRewardListener */ this)
GitHub 上的示例
后续步骤
请浏览以下主题: