激励广告能够向与广告交互的用户提供各种应用内物品作为奖励,如继续游戏、虚拟货币等。这样,用户在广告互动中投入时间后就能获得切实的价值,从而达到推动交互的效果。
以下各节将向您介绍如何加载和显示激励广告。
要加载激励广告,请首先获取对应您的激励广告单位的 MaxRewardedAd 对象实例。
然后调用该对象的 loadAd() 方法。
实现 MaxRewardedAdListener 以便在广告准备就绪时收到通知。这也会通知您其他广告相关的事件。
public class ExampleActivity extends Activity
implements MaxRewardedAdListener
{
private MaxRewardedAd rewardedAd;
private int retryAttempt;
void createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»") );
rewardedAd.setListener( this );
rewardedAd.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error)
{
// Rewarded ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++;
long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable()
{
@Override
public void run()
{
rewardedAd.loadAd();
}
}, delayMillis );
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
rewardedAd.loadAd();
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd();
}
@Override
public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}class ExampleActivity : Activity(), MaxRewardedAdListener
{
private lateinit var rewardedAd: MaxRewardedAd
private var retryAttempt = 0.0
fun createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»" )
rewardedAd.setListener( this )
rewardedAd.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0.0
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?)
{
// Rewarded ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++
val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { rewardedAd.loadAd() }, delayMillis )
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
rewardedAd.loadAd()
}
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd()
}
override fun onUserRewarded(maxAd: MaxAd, maxReward: MaxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}要展示激励广告,请对创建的 MaxRewardedAd 对象调用 showAd( this ) 。
if ( rewardedAd.isReady() )
{
// `this` is the activity that will be used to show the ad
rewardedAd.showAd( this );
}if ( rewardedAd.isReady )
{
// `this` is the activity that will be used to show the ad
rewardedAd.showAd(this);
}要获取奖励金额和币种,请重写 onUserRewarded() 回传:
@Override
public void onUserRewarded(final MaxAd ad, final MaxReward reward)
{
System.out.println( "Rewarded user: " + reward.getAmount() + " " + reward.getLabel() );
}override fun onUserRewarded(ad: MaxAd?, reward: MaxReward?)
{
System.out.println( "Rewarded user: " + reward!!.getAmount() + " " + reward.getLabel() )
}您可以通过货币服务器接收回传。 要了解操作方法,请参阅 MAX S2S Rewarded Callback API 指南。 随后,在Edit Ad Unit页面更新Server Side Callback URL。
要设置激励广告的金额和币种,请按照下列步骤操作:

