After updating Google Ads SDK to 22.2.0 InterstitialAd is deprecated, how to resolve?

113 Views Asked by At

After updating Google Ads SDK to 22.2.0 gives a deprecated warning message for InterstitialAd, while I searched this link for resolving the issue but not succeed. how can I resolve it?

Here is my code

    public static void loadInterstitialAd() {
    String typeOfAds = App.getContext().getResources().getString(R.string.ad_network);
    if (typeOfAds.equals("facebook")) {
        if (interstitialAd == null) {
            interstitialAd = new com.facebook.ads.InterstitialAd(App.getContext(), App.getContext().getResources().getString(R.string.facebook_interstitial_ad_id));
            AttachFacebookListner();
            interstitialAd.loadAd(
                    interstitialAd.buildLoadAdConfig()
                            .withAdListener(interstitialAdListener)
                            .build());
            isAttachedfacebookInterstitial = false;
        } else {
            interstitialAd.loadAd();
        }
    } else if (typeOfAds.equals("admob")) {
        if (interstitialAd == null) {
            interstitialAd = new InterstitialAd(App,getString(R.string));
            interstitialAd.getAdUnitId("admob");
            interstitialAd.loadAd(new AdRequest.Builder().build());
            AttachListner();
            isAttachedInterstitial = false;
        } else if (interstitial_Ad.isLoading()) {
            Log.e("TAG", "loadInterstitialAd: isLoading");
        } else {
            interstitial_Ad.loadAd(new AdRequest.Builder().build());
        }
    }
}

public static void AttachFacebookListner() {
    if (isAttachedInterstitial) {
        interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
                App.stopService();
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
                App.startService();
            }

            @Override
            public void onError(Ad ad, com.facebook.ads.AdError adError) {
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                if (!isShowFacebookInterstitial) {
                    showInterstitialAds();
                }
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        };
    }
}

public static void AttachListner() {
    if (isAttachedInterstitial) {
        interstitial_Ad.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                Log.e("adLoaded", "adLoaded: interstitial");
                if (!isShowInterstitial) {
                    showInterstitialAds();
                }
            }

            @Override
            public void onAdClosed() {
                super.onAdClosed();
                Log.e("onAdClosed", "onAdClosed: ");
                App.startService();
                loadInterstitialAd();
            }

            @Override
            public void onAdImpression() {
                super.onAdImpression();
                Log.e("adOpened", "adOpened: ");
                App.stopService();
            }

            @Override
            public void onAdFailedToLoad(LoadAdError loadAdError) {
                super.onAdFailedToLoad(loadAdError);
                Log.e("adError", "adError: " + loadAdError.toString());
            }
        });
    }
}

public static void showProgressDialog(Context context) {
    if (alertDialog == null) {
        alertDialog = new ProgressDialog(context);
        alertDialog.setTitle("Loading");
        alertDialog.setMessage("Please Wait...");
        alertDialog.setCancelable(false);
    }

    if (alertDialog != null && !alertDialog.isShowing()) {
        alertDialog.show();
    }
}

public static void hideProgressDialog() {
    if (alertDialog != null && alertDialog.isShowing()) {
        alertDialog.dismiss();
    }
}

public static void showInterstitialAds() {
    String typeOfAds = App.getContext().getResources().getString(R.string.ad_network);
    if (typeOfAds.equals("admob")) {
        if (interstitial_Ad != null && interstitial_Ad.isLoaded()) {
            hideProgressDialog();
            interstitial_Ad.show();

            isShowInterstitial = true;
        } else {
            hideProgressDialog();
            isShowInterstitial = false;
        }
    } else if (typeOfAds.equals("facebook")) {
        if (interstitialAd != null && interstitialAd.isAdLoaded()) {
            hideProgressDialog();
            interstitialAd.show();
            isShowFacebookInterstitial = true;
        } else {
            hideProgressDialog();
            isShowFacebookInterstitial = false;
        }
    }
}

public static void showRewardedAds(final Activity context) {
    String typeOfAds = App.getContext().getResources().getString(R.string.ad_network);
    if (typeOfAds.equals("admob")) {
        if (rewarded_ad != null && rewarded_ad.isLoaded()) {
            RewardedAdCallback adCallback = new RewardedAdCallback() {
                @Override
                public void onRewardedAdOpened() {
                    // Ad opened.
                    App.stopService();
                }

                @Override
                public void onRewardedAdClosed() {
                    // Ad closed.
                    App.startService();
                    loadRewardedVideo(context);
                }

                @Override
                public void onUserEarnedReward(@NonNull RewardItem reward) {
                    // User earned reward.
                }

                @Override
                public void onRewardedAdFailedToShow(AdError adError) {
                    // Ad failed to display.
                }
            };
            rewarded_ad.show(context, adCallback);
            isShowRewarded = true;
        } else {
            isShowRewarded = false;
            Constant.showToastMessage(context, "Video Ad not Ready");
        }
    } else if (typeOfAds.equals("facebook")) {
        if (rewardedVideoAd != null && rewardedVideoAd.isAdLoaded()) {
            rewardedVideoAd.show();
            isShowfacebookRewarded = true;
        } else {
            isShowfacebookRewarded = false;
            Constant.showToastMessage(context, "Video Ad not Ready");
        }
    }
}

public static void loadRewardedVideo(final Activity activity) {
    String typeOfAds = App.getContext().getResources().getString(R.string.ad_network);
    if (typeOfAds.equals("admob")) {
        if (rewarded_ad != null) {
            try {
                rewarded_ad = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        rewarded_ad = new RewardedAd(activity, activity.getResources().getString(R.string.admob_rewarded_video_id));
        AttachedRewaredCallBack(activity);
        rewarded_ad.loadAd(new AdRequest.Builder().build(), adLoadCallback);
    } else if (typeOfAds.equals("facebook")) {
        if (rewardedVideoAd != null) {
            try {
                rewardedVideoAd = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        rewardedVideoAd = new RewardedVideoAd(App.getContext(), activity.getResources().getString(R.string.facebook_rewarded_video_id));
        RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {

            @Override
            public void onError(Ad ad, com.facebook.ads.AdError adError) {
                Log.e(TAG, "Rewarded video ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Rewarded video ad is loaded and ready to be displayed
                Log.d(TAG, "Rewarded video ad is loaded and ready to be displayed!");
                if (!isShowfacebookRewarded) {
                    showRewardedAds(activity);
                }

            }

            @Override
            public void onAdClicked(Ad ad) {
                // Rewarded video ad clicked
                Log.d(TAG, "Rewarded video ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Rewarded Video ad impression - the event will fire when the
                // video starts playing
                Log.d(TAG, "Rewarded video ad impression logged!");
                App.stopService();
            }

            @Override
            public void onRewardedVideoCompleted() {
                // Rewarded Video View Complete - the video has been played to the end.
                // You can use this event to initialize your reward
                Log.d(TAG, "Rewarded video completed!");
                App.startService();
                // Call method to give reward
                // giveReward();
            }

            @Override
            public void onRewardedVideoClosed() {
                // The Rewarded Video ad was closed - this can occur during the video
                // by closing the app, or closing the end card.
                Log.d(TAG, "Rewarded video ad closed!");
                App.startService();
            }
        };
        rewardedVideoAd.loadAd(
                rewardedVideoAd.buildLoadAdConfig()
                        .withAdListener(rewardedVideoAdListener)
                        .build());
    }
}`

any answer would be appriaciated!

i've working on two days but i still can't find the solution. thanks in advance!

0

There are 0 best solutions below