gpt4 book ai didi

android - RewardedVideoAd 回调监听器不匹配

转载 作者:行者123 更新时间:2023-12-04 18:18:28 31 4
gpt4 key购买 nike

我已经实现了RewardedVideoAd用于使用 PRO 功能。

当我使用 RewardedVideoAd在整个应用程序中 1 次(在单个屏幕中)然后它可以完美运行。
但是当我多次使用(在多个屏幕中)时,RewardedVideoAd回调监听器是两个屏幕之间的不匹配。

让我解释一下整个场景

  • 我打开了屏幕-A 然后我看了RewardedVideoAd .成功完成此视频后,我获得了奖励。
  • 现在我要去下一个屏幕-B .
  • 那我下一个屏幕-C 屏幕-B 结束。
  • 所以,现在应用程序状态处于前台状态屏幕-C & 处于后台状态 屏幕-A .
  • 现在我正在观看 中的 RewardedVideoAd屏幕-C .成功完成此视频后,我获得了奖励。
  • 然后我关闭了屏幕-C .所以,我重定向到 屏幕-A 处于后台状态。
  • 现在我正在观看 中的 RewardedVideoAd屏幕-A .成功完成此视频后。我没有得到奖励。

  • 所以问题是我上次在 中观看了视频屏幕-C .所以所有的回调都去 屏幕-C ,不在 中屏幕-A .

    我还在所有屏幕中实现了以下代码。
    @Override
    public void onResume() {
    mRewardedVideoAd.resume(this);
    super.onResume();
    }

    @Override
    public void onPause() {
    mRewardedVideoAd.pause(this);
    super.onPause();
    }

    @Override
    public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    super.onDestroy();
    }

    最佳答案

    我制作了一个通用模块来显示 RewardedVideoAd .你可以试试这个。我认为它可以解决你的问题。

    放在项目中的类下面。

    AdRewardedHandler.java

    public class AdRewardedHandler {

    private static final String TAG = "AdRewardedHandler";

    private Context context;
    private static AdRewardedHandler adRewardedHandler;

    private RewardedVideoAd rewardedVideoAd;
    private RewardedAdListener rewardedAdListener;

    private boolean isFailedToLoad = false;


    public static AdRewardedHandler getInstance() {
    if (adRewardedHandler == null) {
    adRewardedHandler = new AdRewardedHandler();
    }
    return adRewardedHandler;
    }

    public void initializeRewardedHandler(Context context) {
    this.context = context;
    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(context);
    loadRewardedVideoAd();
    }

    public void setAdHandlerListener(RewardedAdListener rewardedAdListener1) {
    this.rewardedAdListener = rewardedAdListener1;
    }

    public boolean isAdLoaded() {
    if (rewardedVideoAd != null) {
    return rewardedVideoAd.isLoaded();
    } else {
    return false;
    }
    }

    public void loadRewardedVideoAd() {

    rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
    rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

    @Override
    public void onRewardedVideoAdLoaded() {
    Log.i(TAG, "onRewardedVideoAdLoaded: ");
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoAdLoaded();
    }

    }

    @Override
    public void onRewardedVideoAdOpened() {
    Log.i(TAG, "onRewardedVideoAdOpened: ");

    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoAdOpened();
    }
    }

    @Override
    public void onRewardedVideoStarted() {
    Log.i(TAG, "onRewardedVideoStarted: ");
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoStarted();

    }
    }

    @Override
    public void onRewardedVideoAdClosed() {
    Log.i(TAG, "onRewardedVideoAdClosed: ");
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoAdClosed();
    if (!isAdLoaded()) {
    loadRewardedVideoAd();
    }
    }
    }

    @Override
    public void onRewarded(RewardItem var1) {
    Log.i(TAG, "onRewarded: " + var1);
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewarded(var1);
    }
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
    Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoAdLeftApplication();
    }
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int var1) {
    Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
    if (!isFailedToLoad) {
    isFailedToLoad = true;
    if (!isAdLoaded()) {
    loadRewardedVideoAd();
    }
    }

    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoAdFailedToLoad(var1);

    }


    }

    @Override
    public void onRewardedVideoCompleted() {
    Log.i(TAG, "onRewardedVideoCompleted: ");
    if (rewardedAdListener != null) {
    rewardedAdListener.onRewardedVideoCompleted();
    if (!isAdLoaded()) {
    loadRewardedVideoAd();
    }
    }

    }
    });


    }


    public void showRewardedAd(RewardedAdListener rewardedAdListener1) {

    setAdHandlerListener(rewardedAdListener1);

    Log.i(TAG, "showRewardedAd: ");

    try {
    if (rewardedVideoAd != null && rewardedVideoAd.isLoaded()) {
    rewardedVideoAd.show();
    }
    } catch (Throwable th) {
    th.printStackTrace();
    }

    }

    public void onResume() {
    Log.i(TAG, "onResume: Resume Call :");
    if (rewardedVideoAd != null) {
    rewardedVideoAd.resume(context);
    }
    }

    public void onPause() {
    Log.i(TAG, "onPause: Pause Call : ");
    if (rewardedVideoAd != null) {
    rewardedVideoAd.pause(context);
    }
    }

    public void onDestroy() {
    Log.i(TAG, "onDestroy: Destroy Call :");
    }


    public interface RewardedAdListener {

    void onRewardedVideoAdLoaded();

    void onRewardedVideoAdOpened();

    void onRewardedVideoStarted();

    void onRewardedVideoAdClosed();

    void onRewarded(RewardItem var1);

    void onRewardedVideoAdLeftApplication();

    void onRewardedVideoAdFailedToLoad(int var1);

    void onRewardedVideoCompleted();
    }
    }

    在您的应用程序文件中放入以下行。
    AdRewardedHandler.getInstance().initializeRewardedHandler(this);

    现在,终于如何使用
    public class YourActivity extends AppCompatActivity implements AdRewardedHandler.RewardedAdListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AdRewardedHandler.getInstance().setAdHandlerListener(this);

    if (!AdRewardedHandler.getInstance().isAdLoaded()) {
    AdRewardedHandler.getInstance().loadRewardedVideoAd();
    }

    }

    public void showRewardedVideoAd() {
    // showRewardedAd
    AdRewardedHandler.getInstance().showRewardedAd(this);
    }

    @Override
    public void onResume() {
    AdRewardedHandler.getInstance().onResume();
    super.onResume();
    }


    @Override
    public void onPause() {
    AdRewardedHandler.getInstance().onPause();
    super.onPause();
    }

    @Override
    public void onRewardedVideoAdLoaded() {
    Log.i(TAG, "onRewardedVideoAdLoaded: ");
    }

    @Override
    public void onRewardedVideoAdOpened() {
    Log.i(TAG, "onRewardedVideoAdOpened: ");
    }

    @Override
    public void onRewardedVideoStarted() {
    Log.i(TAG, "onRewardedVideoStarted: ");
    }

    @Override
    public void onRewardedVideoAdClosed() {
    Log.i(TAG, "onRewardedVideoAdClosed: ");
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
    Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int var1) {
    Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
    }

    @Override
    public void onRewardedVideoCompleted() {
    Log.i(TAG, "onRewardedVideoCompleted: ");
    }

    }

    关于android - RewardedVideoAd 回调监听器不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201891/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com