Skip to content

Android Rewarded Video Ad Integration

Overview

Rewarded video ads are full-screen video ads where users can get rewards after watching the complete video, with the following characteristics:

  • High user engagement
  • High revenue potential
  • Good user experience
  • Suitable for games and utility applications

Integration Steps

Refer to the RewardAdHelper in the demo example

1. Initialize Rewarded Video Ad

Load rewarded video ads in Activity or Fragment:

kotlin
class RewardAdHelper(private val activity: Activity, private val logger: PrintLogger) {
    private var rewardAdObject: RewardAdObject? = null

    /**
     * Load rewarded ad
     * 
     * This method creates ad configuration, initializes rewarded ad object, sets ad listener
     * Finally calls load() method to start loading ads
     */
    fun load() {
        // Create ad configuration object, set placement ID
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.REWARD_ID, // Rewarded ad placement ID
        )
        
        // Initialize rewarded ad object
        rewardAdObject = RewardAdObject(activity, adConfig)
        
        // Set ad listener to monitor various ad events
        rewardAdObject?.setAdObjectListener(object : RewardAdObjectListener {
            /**
             * Ad loaded successfully callback
             * 
             * @param placementId Ad placement ID
             */
            override fun onLoadSuccess(placementId: String) {
                // Ad loaded successfully, can be displayed now, recommended to check isReady before display
                logger.add("Reward: onLoadSuccess")
            }

            /**
             * Ad loading failed callback
             * 
             * @param error Error information
             * @param placementId Ad placement ID
             */
            override fun onLoadError(
                error: UjuException,
                placementId: String
            ) {
                // Record loading error information
                logger.add("Reward: onLoadError:${error.message}")
                DemoLogUtils.e("Reward: onLoadError:${error.message}")
            }

            /**
             * Ad displayed successfully callback
             */
            override fun onAdShow() {
                // Get ad information, record ecpm value
                val adInfo = rewardAdObject?.getAdInfo()
                logger.add("Reward: onAdShow:ecpm${adInfo?.ecpm}")
            }

            /**
             * Ad display error callback
             * 
             * @param error Error information
             * @param placementId Ad placement ID
             */
            override fun onAdError(
                error: UjuException,
                placementId: String
            ) {
                // Record ad error information
                logger.add("Reward: onAdError:${error.message}")
                DemoLogUtils.e("onAdError:${error.message}")
            }

            /**
             * Ad playback completed callback
             */
            override fun onAdPlayComplete() {
                // Record ad playback completion event
                logger.add("Reward: onAdPlayComplete")
            }

            /**
             * Ad clicked callback
             */
            override fun onAdClicked() {
                // Record ad click event
                logger.add("Reward: onAdClicked")
            }

            /**
             * User skipped video callback
             */
            override fun onAdSkippedVideo() {
                // Record user skipped video event
                logger.add("Reward: onAdSkippedVideo")
            }

            /**
             * Reward arrived callback
             * 
             * When user finishes watching the ad, this callback will be triggered, issue rewards here
             */
            override fun onAdRewardArrived() {
                // Record reward arrival event
                logger.add("Reward: onAdRewardArrived")
                // Note: In actual applications, rewards should be issued to users in this callback
            }

            /**
             * Ad closed callback
             */
            override fun onAdClosed() {
                // Destroy ad object, release resources
                rewardAdObject?.destroy()
                rewardAdObject = null
                // Record ad close event
                logger.add("Reward: onAdClosed")
            }
        })
        
        // Start loading ad
        rewardAdObject?.load()
        // Record ad loading event
        logger.add("Reward: load, placementId:${adConfig.placementId}")
        DemoLogUtils.d("Reward: load, placementId:${adConfig.placementId}")
    }

    /**
     * Display rewarded ad
     * 
     * Before displaying the ad, it checks if the ad is ready
     * Only when the ad status is ready, it calls show() method to display the ad
     */
    fun show() {
        // Check if ad is ready
        if (rewardAdObject?.isReady() == true) {
            // Ad is ready, display ad
            rewardAdObject?.show(activity)
        } else {
            // Ad is not ready, record log
            logger.add("Reward: Ad not ready yet")
        }
    }

    /**
     * Check if ad is loaded
     * 
     * @return true if ad object is created, false otherwise
     * Note: This method only checks if ad object exists, does not guarantee ad is ready
     * To check if ad can be displayed, use isReady() method
     */
    fun isLoaded(): Boolean {
        return rewardAdObject != null
    }

    /**
     * Destroy ad object
     * 
     * When ad is no longer needed, call this method to destroy ad object and release resources
     */
    fun destroy() {
        // Destroy ad object
        rewardAdObject?.destroy()
        // Set ad object reference to null
        rewardAdObject = null
    }

}

2. Display Rewarded Video Ad

When the user triggers the behavior of watching an ad (such as clicking the "Watch ad to get reward" button), display the ad:

kotlin
fun show() {
    if (rewardAdObject?.isReady() == true) {
        rewardAdObject?.show(activity)
    } else {
        logger.add("Reward: Ad not ready yet")
    }
}

3. Issue Rewards

Issue rewards in the onAdRewardArrived callback:

kotlin
fun grantReward(reward RewardItem) {
    // Issue rewards based on reward type and amount
    val rewardType: String = reward.getType()
    val rewardAmount: Int = reward.getAmount()

    when (rewardType) {
        "coins" -> {
            // Issue coins
            userCoins += rewardAmount
            updateCoinsDisplay()
        }

        "lives" -> {
            // Issue lives
            userLives += rewardAmount
            updateLivesDisplay()
        }

        "points" -> {
            // Issue points
            userPoints += rewardAmount
            updatePointsDisplay()
        }

        else -> {}
    }
    
    // Show reward notification
}

4. Ad Lifecycle Management

Manage ad status in Activity or Fragment lifecycle methods:

kotlin
/**
 * Called when Activity is paused
 * 
 * Pause ads in this method to save resources
 */
override fun onPause() {
    super.onPause()
    // Pause ads
    if (rewardAdObject != null) {
        rewardAdObject?.pause()
    }
}

/**
 * Called when Activity is resumed
 * 
 * Resume ads in this method so ads can display normally
 */
override fun onResume() {
    super.onResume()
    // Resume ads
    if (rewardAdObject != null) {
        rewardAdObject?.resume()
    }
}

/**
 * Called when Activity is destroyed
 * 
 * Destroy ads in this method to release resources and avoid memory leaks
 */
override fun onDestroy() {
    super.onDestroy()
    // Destroy ads
    if (rewardAdObject != null) {
        rewardAdObject?.destroy()
        rewardAdObject = null
    }
}

Best Practices

1. Ad Trigger Timing

  • After game level failure: Provide option to watch ad to continue playing
  • When resources are insufficient: Provide option to watch ad to get additional resources
  • Before unlocking content: Provide option to watch ad to unlock premium content
  • Daily rewards: Provide option to watch ad to get extra daily rewards

2. Optimization Suggestions

  • Preload ads: Load ads in advance before they need to be displayed
  • Ad status check: Check if ads are loaded before displaying
  • Reward issuance: Ensure reliability of reward issuance
  • User experience: Provide clear reward notifications
  • Frequency control: Avoid over-displaying ads

3. Avoidable Issues

  • Don't force ads to be displayed during user's critical operations
  • Don't falsely advertise rewards
  • Don't affect normal application functions when ad loading fails
  • Don't issue rewards when users haven't finished watching

Common Questions

Q: Why isn't the rewarded video ad displaying?

A: Possible reasons:

  • Incorrect ad unit ID
  • Network connection issues
  • Insufficient ad inventory
  • Ad not loaded completely
  • Device restrictions

Q: How to ensure the reliability of reward issuance?

A: Recommendations:

  • Issue rewards in the onUserEarnedReward callback
  • Server-side verification of reward validity
  • Local storage of reward records to prevent unexpected situations
  • Provide retry mechanism for failed reward issuance

Q: How to improve rewarded video ad revenue?

A: Recommendations:

  • Choose appropriate ad trigger timing
  • Provide attractive rewards
  • Optimize ad display frequency
  • Ensure ads are relevant to application content
  • Increase application user activity