RewardedVideoAd 回调监听器不匹配

RewardedVideoAd callback listener mismatch

我已经实施 RewardedVideoAd 以使用 PRO 功能。

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

让我解释一下整个场景

  1. 我打开了SCREEN-A然后我看了RewardedVideoAd。成功完成此视频后,我获得了奖励。
  2. 现在我要进入下一个 SCREEN-B
  3. 然后,我将进行下一个 SCREEN-CSCREEN-B 完成。
  4. 所以,现在应用程序状态处于前台状态 SCREEN-C & 处于后台状态 SCREEN-A.
  5. 现在我正在 SCREEN-C 中观看 RewardedVideoAd。成功完成此视频后,我获得了奖励。
  6. 然后我关闭了SCREEN-C。所以,我重定向到处于后台状态的SCREEN-A
  7. 现在我正在 SCREEN-A 中观看 RewardedVideoAd。成功完成此视频后。我没有得到奖励。

所以问题是我上次在 SCREEN-C 中观看了视频。所以所有回调都将转到 SCREEN-C,而不是 SCREEN-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();
}

我认为它与这个问题有关Rewarded Video Ads never die。如果它是相同的问题,您可以作为解决方法解决此问题:

you should explicitly unset the listener for the RewardedVideoAd before you call destroy, as destroy will not do this for you.

我做了一个通用的展示模块RewardedVideoAd。你可以试试这个。我想这可能会解决你的问题。

在你的项目中放在 class 下面。

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

}

我遇到了同样的问题。就我而言,当我要去 Activity A 到 B 时,我的 Activity A 在后台,Activity B 在前台,现在当我回到 Activity 那时我没有收到 RewardedVideoAds 的回调。

我用简单的方法解决了这个问题,当我回到 Activity 那个时候我在 OnResume 方法中设置了监听器。

 @Override
 public void onResume() {
    super.onResume();
    Log.i(TAG, "onResume Call.");

    if (rewardedVideoAd != null) {
        rewardedVideoAd.setRewardedVideoAdListener(this);
        rewardedVideoAd.resume(activity);
    }
}

请注意,对于此解决方案,您必须从 Activity B 中删除这两行。

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "onDestroy: ");
    rewardedVideoAd.destroy(activity);
    rewardedVideoAd.setRewardedVideoAdListener(null);
}