Rewarded
Integration
Just as interstitials, rewarded ads can only be integrated with code. Below we show a simple example to understand how it works:
public class MainActivity extends Activity {
private RewardedAd mRewarded;
@Override
public void onCreate(Bundle savedInstanceState) {
...
mRewarded = new RewardedAd(this, "Wortise Ad Unit ID");
mRewarded.loadAd();
}
@Override
public void onDestroy() {
...
mRewarded.destroy();
}
public void showInterstitial() {
if (mRewarded.isAvailable()) {
mRewarded.showAd(this);
}
}
}
class MainActivity : Activity() {
private var interstitialAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
...
interstitialAd = InterstitialAd(this, "<Wortise Ad Unit ID>").also {
it.loadAd()
}
}
override fun onDestroy() {
...
interstitialAd?.destroy()
}
fun showInterstitial() {
if (interstitialAd?.isAvailable == true) {
interstitialAd?.showAd(this)
}
}
}
Listener configuration
Like occurs with other formats, a listener can be set to receive the events that happen during the rewarded lifecycle. For this, it is needed to implement the RewardedAd.Listener
interface just as shown in the example below:
mRewarded.setListener(new RewardedAd.Listener() {
@Override
public void onRewardedClicked(@NonNull RewardedAd ad) {
// Invoked when the ad has been clicked
}
@Override
public void onRewardedCompleted(@NonNull RewardedAd ad,
@NonNull Reward reward) {
// Invoked when the ad has been completed
}
@Override
public void onRewardedDismissed(@NonNull RewardedAd ad) {
// Invoked when the ad has been dismissed
}
@Override
public void onRewardedFailedToLoad(@NonNull RewardedAd ad,
@NonNull AdError error) {
// Invoked when the ad could not be loaded
// (because of an error or no fill)
}
@Override
public void onRewardedFailedToShow(@NonNull RewardedAd ad,
@NonNull AdError error) {
// Invoked when the ad could not be shown
}
@Override
public void onRewardedImpression(@NonNull RewardedAd ad) {
// Invoked when the ad has generated an impression
}
@Override
public void onRewardedLoaded(@NonNull RewardedAd ad) {
// Invoked when the ad has been loaded
}
@Override
public void onRewardedShown(@NonNull RewardedAd ad) {
// Invoked when the ad has been shown
}
});
rewardedAd.listener = object : RewardedAd.Listener() {
override fun onRewardedClicked(ad: RewardedAd) {
// Invoked when the ad has been clicked
}
override fun onRewardedCompleted(ad: RewardedAd, reward: Reward) {
// Invoked when the ad has been completed
}
override fun onRewardedDismissed(ad: RewardedAd) {
// Invoked when the ad has been dismissed
}
override fun onRewardedFailedToLoad(ad: RewardedAd, error: AdError) {
// Invoked when the ad could not be loaded
// (because of an error or no fill)
}
override fun onRewardedFailedToShow(ad: RewardedAd, error: AdError) {
// Invoked when the ad could not be shown
}
override fun onRewardedImpression(ad: RewardedAd) {
// Invoked when the ad has generated an impression
}
override fun onRewardedLoaded(ad: RewardedAd) {
// Invoked when the ad has been loaded
}
override fun onRewardedShown(ad: RewardedAd) {
// Invoked when the ad has been shown
}
}
Última actualización