Search in sources :

Example 1 with InterstitialAdLoadCallback

use of com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback in project mopub-android-mediation by mopub.

the class GooglePlayServicesInterstitial method load.

@Override
protected void load(@NonNull final Context context, @NonNull final AdData adData) {
    Preconditions.checkNotNull(context);
    Preconditions.checkNotNull(adData);
    mContext = context;
    setAutomaticImpressionAndClickTracking(false);
    final Map<String, String> extras = adData.getExtras();
    if (extrasAreValid(extras)) {
        mAdUnitId = extras.get(AD_UNIT_ID_KEY);
        mGooglePlayServicesAdapterConfiguration.setCachedInitializationParameters(context, extras);
    } else {
        MoPubLog.log(getAdNetworkId(), LOAD_FAILED, ADAPTER_NAME, MoPubErrorCode.NETWORK_NO_FILL.getIntCode(), MoPubErrorCode.NETWORK_NO_FILL);
        if (mLoadListener != null) {
            mLoadListener.onAdLoadFailed(MoPubErrorCode.NETWORK_NO_FILL);
        }
        return;
    }
    final AdRequest.Builder builder = new AdRequest.Builder();
    builder.setRequestAgent("MoPub");
    // Publishers may append a content URL by passing it to the MoPubInterstitial.setLocalExtras() call.
    final String contentUrl = extras.get(CONTENT_URL_KEY);
    if (!TextUtils.isEmpty(contentUrl)) {
        builder.setContentUrl(contentUrl);
    }
    forwardNpaIfSet(builder);
    final RequestConfiguration.Builder requestConfigurationBuilder = new RequestConfiguration.Builder();
    // Publishers may request for test ads by passing test device IDs to the MoPubView.setLocalExtras() call.
    final String testDeviceId = extras.get(TEST_DEVICES_KEY);
    if (!TextUtils.isEmpty(testDeviceId)) {
        requestConfigurationBuilder.setTestDeviceIds(Collections.singletonList(testDeviceId));
    }
    // Publishers may want to indicate that their content is child-directed and forward this
    // information to Google.
    final String childDirected = extras.get(TAG_FOR_CHILD_DIRECTED_KEY);
    if (childDirected != null) {
        if (Boolean.parseBoolean(childDirected)) {
            requestConfigurationBuilder.setTagForChildDirectedTreatment(TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE);
        } else {
            requestConfigurationBuilder.setTagForChildDirectedTreatment(TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE);
        }
    } else {
        requestConfigurationBuilder.setTagForChildDirectedTreatment(TAG_FOR_CHILD_DIRECTED_TREATMENT_UNSPECIFIED);
    }
    // Publishers may want to mark their requests to receive treatment for users in the
    // European Economic Area (EEA) under the age of consent.
    final String underAgeOfConsent = extras.get(TAG_FOR_UNDER_AGE_OF_CONSENT_KEY);
    if (underAgeOfConsent != null) {
        if (Boolean.parseBoolean(underAgeOfConsent)) {
            requestConfigurationBuilder.setTagForUnderAgeOfConsent(TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE);
        } else {
            requestConfigurationBuilder.setTagForUnderAgeOfConsent(TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE);
        }
    } else {
        requestConfigurationBuilder.setTagForUnderAgeOfConsent(TAG_FOR_UNDER_AGE_OF_CONSENT_UNSPECIFIED);
    }
    final RequestConfiguration requestConfiguration = requestConfigurationBuilder.build();
    MobileAds.setRequestConfiguration(requestConfiguration);
    final AdRequest adRequest = builder.build();
    InterstitialAd.load(context, mAdUnitId, adRequest, new InterstitialAdLoadCallback() {

        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
            Preconditions.checkNotNull(interstitialAd);
            mGoogleInterstitialAd = interstitialAd;
            MoPubLog.log(getAdNetworkId(), LOAD_SUCCESS, ADAPTER_NAME);
            if (mLoadListener != null) {
                mLoadListener.onAdLoaded();
            }
            mGoogleInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                @Override
                public void onAdDismissedFullScreenContent() {
                    if (mInteractionListener != null) {
                        mInteractionListener.onAdDismissed();
                    }
                    mGoogleInterstitialAd = null;
                }

                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    Preconditions.checkNotNull(adError);
                    MoPubLog.log(getAdNetworkId(), CUSTOM, ADAPTER_NAME, "Failed to show " + "Google interstitial. " + adError.getMessage());
                    MoPubLog.log(getAdNetworkId(), SHOW_FAILED, ADAPTER_NAME, MoPubErrorCode.NETWORK_NO_FILL.getIntCode(), MoPubErrorCode.NETWORK_NO_FILL);
                    if (mInteractionListener != null) {
                        mInteractionListener.onAdFailed(MoPubErrorCode.NETWORK_NO_FILL);
                    }
                    mGoogleInterstitialAd = null;
                }

                @Override
                public void onAdShowedFullScreenContent() {
                    MoPubLog.log(getAdNetworkId(), SHOW_SUCCESS, ADAPTER_NAME);
                    if (mInteractionListener != null) {
                        mInteractionListener.onAdShown();
                    }
                }

                @Override
                public void onAdImpression() {
                    if (mInteractionListener != null) {
                        mInteractionListener.onAdImpression();
                    }
                }
            });
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            Preconditions.checkNotNull(loadAdError);
            MoPubLog.log(getAdNetworkId(), LOAD_FAILED, ADAPTER_NAME, MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR.getIntCode(), MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
            MoPubLog.log(getAdNetworkId(), CUSTOM, ADAPTER_NAME, "Failed to load Google " + "interstitial. " + loadAdError.getMessage());
            if (mLoadListener != null) {
                mLoadListener.onAdLoadFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
            }
        }
    });
    MoPubLog.log(getAdNetworkId(), LOAD_ATTEMPTED, ADAPTER_NAME);
}
Also used : RequestConfiguration(com.google.android.gms.ads.RequestConfiguration) LoadAdError(com.google.android.gms.ads.LoadAdError) AdRequest(com.google.android.gms.ads.AdRequest) FullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback) NonNull(androidx.annotation.NonNull) InterstitialAdLoadCallback(com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback) InterstitialAd(com.google.android.gms.ads.interstitial.InterstitialAd) LoadAdError(com.google.android.gms.ads.LoadAdError) AdError(com.google.android.gms.ads.AdError)

Example 2 with InterstitialAdLoadCallback

use of com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback in project quickstart-android by firebase.

the class FirstFragment method requestNewInterstitial.

/**
 * Load a new interstitial ad asynchronously.
 */
private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    // AdMob ad unit IDs are not currently stored inside the google-services.json file.
    // Developers using AdMob can store them as custom values in a string resource file or
    // simply use constants. Note that the ad units used here are configured to return only test
    // ads, and should not be used outside this sample.
    InterstitialAd.load(getContext(), getString(R.string.interstitial_ad_unit_id), adRequest, new InterstitialAdLoadCallback() {

        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
            super.onAdLoaded(interstitialAd);
            mInterstitialAd = interstitialAd;
            // Ad received, ready to display
            if (mLoadInterstitialButton != null) {
                mLoadInterstitialButton.setEnabled(true);
            }
            mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                @Override
                public void onAdDismissedFullScreenContent() {
                    super.onAdDismissedFullScreenContent();
                    requestNewInterstitial();
                    beginSecondActivity();
                }
            });
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            super.onAdFailedToLoad(loadAdError);
            mInterstitialAd = null;
            Log.w(TAG, "onAdFailedToLoad:" + loadAdError.getMessage());
        }
    });
}
Also used : AdRequest(com.google.android.gms.ads.AdRequest) LoadAdError(com.google.android.gms.ads.LoadAdError) FullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback) InterstitialAdLoadCallback(com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback) InterstitialAd(com.google.android.gms.ads.interstitial.InterstitialAd)

Example 3 with InterstitialAdLoadCallback

use of com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback in project googleads-mobile-android-mediation by googleads.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapterRadioButton = findViewById(R.id.integration_adapter);
    // Banner ads.
    Button loadBannerButton = findViewById(R.id.banner_load_ad);
    loadBannerButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            adView = new AdView(view.getContext());
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(getBannerAdUnitId());
            adView.setAdListener(new AdListener() {

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load banner: " + loadAdError, Toast.LENGTH_SHORT).show();
                }
            });
            adView.loadAd(new AdRequest.Builder().build());
            // Add banner to view hierarchy.
            FrameLayout bannerContainer = findViewById(R.id.banner_container);
            bannerContainer.removeAllViews();
            bannerContainer.addView(adView);
        }
    });
    // Interstitial ads.
    loadInterstitialButton = (Button) findViewById(R.id.interstitial_load_button);
    loadInterstitialButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            loadInterstitialButton.setEnabled(false);
            InterstitialAd.load(MainActivity.this, getInterstitialAdUnitId(), new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    interstitial = interstitialAd;
                    interstitial.setFullScreenContentCallback(new FullScreenContentCallback() {

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError error) {
                            Toast.makeText(MainActivity.this, "Failed to show interstitial: " + error, Toast.LENGTH_SHORT).show();
                            loadInterstitialButton.setEnabled(true);
                        }

                        @Override
                        public void onAdDismissedFullScreenContent() {
                            loadInterstitialButton.setEnabled(true);
                        }
                    });
                    showInterstitialButton.setEnabled(true);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load interstitial: " + loadAdError, Toast.LENGTH_SHORT).show();
                    interstitial = null;
                    loadInterstitialButton.setEnabled(true);
                }
            });
        }
    });
    showInterstitialButton = (Button) findViewById(R.id.interstitial_show_button);
    showInterstitialButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            showInterstitialButton.setEnabled(false);
            if (interstitial != null) {
                interstitial.show(MainActivity.this);
            }
        }
    });
    // Sample Adapter Rewarded Ad Button.
    loadRewardedButton = (Button) findViewById(R.id.rewarded_load_button);
    loadRewardedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            loadRewardedButton.setEnabled(false);
            RewardedAd.load(MainActivity.this, getRewardedAdUnitId(), new AdRequest.Builder().build(), new RewardedAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull RewardedAd ad) {
                    rewardedAd = ad;
                    rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError error) {
                            Toast.makeText(MainActivity.this, "Failed to show interstitial: " + error, Toast.LENGTH_SHORT).show();
                            loadRewardedButton.setEnabled(true);
                        }

                        @Override
                        public void onAdDismissedFullScreenContent() {
                            loadRewardedButton.setEnabled(true);
                        }
                    });
                    showRewardedButton.setEnabled(true);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load rewarded ad: " + loadAdError, Toast.LENGTH_SHORT).show();
                    rewardedAd = null;
                    loadRewardedButton.setEnabled(true);
                }
            });
        }
    });
    showRewardedButton = (Button) findViewById(R.id.rewarded_show_button);
    showRewardedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            showRewardedButton.setEnabled(false);
            if (rewardedAd != null) {
                rewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() {

                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                        Toast.makeText(MainActivity.this, String.format("User earned reward. Type: %s, amount: %d", rewardItem.getType(), rewardItem.getAmount()), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    });
    // Native ads.
    final Button nativeLoadButton = (Button) findViewById(R.id.native_load_ad);
    nativeLoadButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            adLoader = new AdLoader.Builder(view.getContext(), getNativeAdUnitId()).forNativeAd(new NativeAd.OnNativeAdLoadedListener() {

                @Override
                public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
                    FrameLayout nativeContainer = findViewById(R.id.native_container);
                    NativeAdView adView = (NativeAdView) getLayoutInflater().inflate(R.layout.native_ad, null);
                    populateNativeAdView(nativeAd, adView);
                    nativeContainer.removeAllViews();
                    nativeContainer.addView(adView);
                }
            }).withAdListener(new AdListener() {

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError error) {
                    Toast.makeText(MainActivity.this, "Failed to load native ad: " + error, Toast.LENGTH_SHORT).show();
                }
            }).build();
            adLoader.loadAd(new AdRequest.Builder().build());
        }
    });
}
Also used : LoadAdError(com.google.android.gms.ads.LoadAdError) OnUserEarnedRewardListener(com.google.android.gms.ads.OnUserEarnedRewardListener) RewardedAdLoadCallback(com.google.android.gms.ads.rewarded.RewardedAdLoadCallback) AdRequest(com.google.android.gms.ads.AdRequest) RadioButton(android.widget.RadioButton) Button(android.widget.Button) RewardedAd(com.google.android.gms.ads.rewarded.RewardedAd) NonNull(androidx.annotation.NonNull) InterstitialAdLoadCallback(com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback) RewardItem(com.google.android.gms.ads.rewarded.RewardItem) InterstitialAd(com.google.android.gms.ads.interstitial.InterstitialAd) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) AdView(com.google.android.gms.ads.AdView) LoadAdError(com.google.android.gms.ads.LoadAdError) AdError(com.google.android.gms.ads.AdError) AdLoader(com.google.android.gms.ads.AdLoader) OnClickListener(android.view.View.OnClickListener) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) ImageView(android.widget.ImageView) View(android.view.View) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) AdView(com.google.android.gms.ads.AdView) TextView(android.widget.TextView) MediaView(com.google.android.gms.ads.nativead.MediaView) AdListener(com.google.android.gms.ads.AdListener) NativeAd(com.google.android.gms.ads.nativead.NativeAd) FrameLayout(android.widget.FrameLayout) FullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback) OnClickListener(android.view.View.OnClickListener)

Example 4 with InterstitialAdLoadCallback

use of com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback in project godot-admob-android by Poing-Studios.

the class AdMob method load_interstitial.

// BANNER
// INTERSTITIAL
public void load_interstitial(final String pAdUnitId) {
    aActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (aIsInitialized) {
                InterstitialAd.load(aActivity, pAdUnitId, getAdRequest(), new InterstitialAdLoadCallback() {

                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // Code to be executed when an ad finishes loading.
                        aInterstitialAd = interstitialAd;
                        emitSignal("interstitial_loaded");
                        aIsInterstitialLoaded = true;
                        interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when fullscreen content is dismissed.
                                aInterstitialAd = null;
                                emitSignal("interstitial_closed");
                                aIsInterstitialLoaded = false;
                            }

                            @Override
                            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                                // Called when fullscreen content failed to show.
                                aInterstitialAd = null;
                                emitSignal("interstitial_failed_to_show", adError.getCode());
                                aIsInterstitialLoaded = false;
                            }

                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when fullscreen content is shown.
                                emitSignal("interstitial_opened");
                            }
                        });
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError adError) {
                        // Code to be executed when an ad request fails.
                        aInterstitialAd = null;
                        emitSignal("interstitial_failed_to_load", adError.getCode());
                    }
                });
            }
        }
    });
}
Also used : LoadAdError(com.google.android.gms.ads.LoadAdError) NonNull(androidx.annotation.NonNull) FullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback) InterstitialAdLoadCallback(com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback) RewardedInterstitialAdLoadCallback(com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback) InterstitialAd(com.google.android.gms.ads.interstitial.InterstitialAd) RewardedInterstitialAd(com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd) LoadAdError(com.google.android.gms.ads.LoadAdError) AdError(com.google.android.gms.ads.AdError)

Aggregations

FullScreenContentCallback (com.google.android.gms.ads.FullScreenContentCallback)4 LoadAdError (com.google.android.gms.ads.LoadAdError)4 InterstitialAd (com.google.android.gms.ads.interstitial.InterstitialAd)4 InterstitialAdLoadCallback (com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback)4 NonNull (androidx.annotation.NonNull)3 AdError (com.google.android.gms.ads.AdError)3 AdRequest (com.google.android.gms.ads.AdRequest)3 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1 FrameLayout (android.widget.FrameLayout)1 ImageView (android.widget.ImageView)1 RadioButton (android.widget.RadioButton)1 TextView (android.widget.TextView)1 AdListener (com.google.android.gms.ads.AdListener)1 AdLoader (com.google.android.gms.ads.AdLoader)1 AdView (com.google.android.gms.ads.AdView)1 OnUserEarnedRewardListener (com.google.android.gms.ads.OnUserEarnedRewardListener)1 RequestConfiguration (com.google.android.gms.ads.RequestConfiguration)1 MediaView (com.google.android.gms.ads.nativead.MediaView)1