0

Hi I am creating an Android app. I want to show Interstitial ads after some time of app load. Say 60 seconds.

The Code that I had developed shows add instantly as soon as some button or icon is click, I need that it should be shown after a delay of say 60 sec. But in the mean time the App should perform the operation click and should not wait or sleep.



public static void showFANInterstitialAds(Activity context) {
    final String TAG = "FAN";
    if (!PreferenceUtils.isActivePlan(context)) {
        DatabaseHelper db = new DatabaseHelper(context);
        String placementId = db.getConfigurationData().getAdsConfig().getFanInterstitialAdsPlacementId();

        final com.facebook.ads.InterstitialAd interstitialAd = new com.facebook.ads.InterstitialAd(context, placementId);
        InterstitialAdListener listener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

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

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                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
                interstitialAd.show();
            }

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

        interstitialAd.loadAd(interstitialAd.buildLoadAdConfig()
                .withAdListener(listener)
                .build());
    }

}


Help me to resolve my issue.

1 Answers1

1

Use Handler to delay the show method.

  @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
                new Handler().postDelayed(60000,new Runnable() {
                   @Override
                   public void run() {
                     interstitialAd.show();
                   }
              });
                
            }
Alpha 1
  • 4,118
  • 2
  • 17
  • 23