Search code examples
androidadsdeprecatedadmob-rewardedvideoadgoogle-ads-api

RewardedVideoAd MobileAds.getRewardedVideoAdInstance(this) is deprecated. What is the new alternative?


RewardedVideoAd is deprecated. What is the new alternative?

    void loadAds(){

        RewardedVideoAd mRewardedVideoAd;
        mRewardedVideoAd.loadAd(rewarded_id, new AdRequest.Builder().build());
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);

        mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            AppUtils.showLog("REWARDED", "onRewardedVideoAdLoaded");
        }
        .....
        .....

    }

Solution

  • This is the new way to implement new RewardVideoAds. Found via this official website.

    Load an ad

    import com.google.android.gms.ads.rewarded.RewardedAd;
    
    public class MainActivity extends Activity {
    
    private RewardedAd rewardedAd;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        rewardedAd = new RewardedAd(this,
            "ca-app-pub-3940256099942544/5224354917");
    
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }
    
           @Override
            public void onRewardedAdFailedToLoad(LoadAdError adError) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
    }
    }
    

    Show the ad

    myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (rewardedAd.isLoaded()) {
            Activity activityContext = MainActivity.this
            RewardedAdCallback adCallback = new RewardedAdCallback() {
                @Override
                public void onRewardedAdOpened() {
                    // Ad opened.
                }
    
                @Override
                public void onRewardedAdClosed() {
                    // Ad closed.
                }
    
                @Override
                public void onUserEarnedReward(@NonNull RewardItem reward) {
                    // User earned reward.
                }
    
                @Override
                public void onRewardedAdFailedToShow(AdError adError) {
                    // Ad failed to display.
                }
            };
            rewardedAd.show(activityContext, adCallback);
        } else {
            Log.d("TAG", "The rewarded ad wasn't loaded yet.");
        }
    }
    });
    

    Using RewardedAdCallback to preload the next rewarded ad

    public RewardedAd createAndLoadRewardedAd() {
        RewardedAd rewardedAd = new RewardedAd(this,
                "ca-app-pub-3940256099942544/5224354917");
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }
    
            @Override
            public void onRewardedAdFailedToLoad(LoadAdError adError) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
        return rewardedAd;
    }
    
    @Override
    public void onRewardedAdClosed() {
        this.rewardedAd = createAndLoadRewardedAd();
    }