RewardedVideoAd MobileAds.getRewardedVideoAdInstance(this) 已弃用。什么是新的选择?

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

RewardedVideoAd 已弃用。新的替代品是什么?

    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");
        }
        .....
        .....

    }

现在应该改用新的 class RewardedAd。参见 the Google API docs here

这是实现新 RewardVideoAds 的新方法。通过此 official website.

找到

加载广告

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);
}
}

显示广告

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.");
    }
}
});

使用 RewardedAdCallback 预加载下一个激励广告

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();
}