Java Code Examples for com.google.android.gms.ads.InterstitialAd#setAdListener()

The following examples show how to use com.google.android.gms.ads.InterstitialAd#setAdListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YTutils.java    From YTPlayer with GNU General Public License v3.0 6 votes vote down vote up
public static void showAd(Context con) {

        try {
            InterstitialAd mInterstitialAd = new InterstitialAd(con);
            mInterstitialAd.setAdUnitId("ca-app-pub-1164424526503510/4801416648");
            mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice("07153BA64BB64F7C3F726B71C4AE30B9").build());
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    Log.e(TAG, "onAdFailedToLoad: Ad failed to load: " + i);
                }

                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    mInterstitialAd.show();
                }
            });
        } catch (Exception ignored) {
        }
    }
 
Example 2
Source File: YTutils.java    From YTPlayer with GNU General Public License v3.0 6 votes vote down vote up
public static void showInterstitialAd(Context activity) {
    if (!AppSettings.showAds)
        return;
    //TODO: Change ad unit ID, Sample ca-app-pub-3940256099942544/1033173712
    InterstitialAd mInterstitialAd = new InterstitialAd(activity);
    mInterstitialAd.setAdUnitId("ca-app-pub-1164424526503510/4801416648");
    mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice("07153BA64BB64F7C3F726B71C4AE30B9").build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int i) {
            super.onAdFailedToLoad(i);
            Log.e(TAG, "onAdFailedToLoad: Ad failed to load: " + i);
        }

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            mInterstitialAd.show();
        }
    });
}
 
Example 3
Source File: FragmentLocationActivity.java    From GooglePlayServiceLocationSupport with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_location);
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad));
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            finish();
        }
    });
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
 
Example 4
Source File: GooglePlayServicesInterstitial.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAd(MediatedInterstitialAdViewController mIC, Activity activity,
                      String parameter, String adUnitId, TargetingParameters targetingParameters) {
    adListener = new GooglePlayAdListener(mIC, super.getClass().getSimpleName());
    adListener.printToClog(String.format(" - requesting an ad: [%s, %s]", parameter, adUnitId));

    interstitialAd = new InterstitialAd(activity);
    interstitialAd.setAdUnitId(adUnitId);
    interstitialAd.setAdListener(adListener);

    try {
        interstitialAd.loadAd(buildRequest(targetingParameters));
    } catch (NoClassDefFoundError e) {
        // This can be thrown by Play Services on Honeycomb.
        adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
    }
}
 
Example 5
Source File: SongBroadCast.java    From YTPlayer with GNU General Public License v3.0 6 votes vote down vote up
void showAd(Context con) {
    if (AppSettings.playAdCount%AppSettings.adOffset==0 && AppSettings.playAdCount!=0 && AppSettings.showAds) {
        Log.e(TAG, "showAd: Showing Ad..." );
        //TODO: Change ad unit ID, Sample ca-app-pub-3940256099942544/1033173712
        mInterstitialAd = new InterstitialAd(con);
        mInterstitialAd.setAdUnitId("ca-app-pub-1164424526503510/4801416648");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                Log.e(TAG, "onAdFailedToLoad: Ad failed to load: " + i);
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mInterstitialAd.show();
            }
        });
    }
}
 
Example 6
Source File: GenerateShortcutHelper.java    From TvAppRepo with Apache License 2.0 5 votes vote down vote up
static InterstitialAd showVisualAd(Activity activity) {
    final InterstitialAd video =  new InterstitialAd(activity);
    video.setAdUnitId(activity.getString(R.string.interstitial_ad_unit_id));
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    video.loadAd(adRequest);
    Log.d(TAG, "Loading ad");

    video.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            Log.d(TAG, "Ad loaded");
            // Show video as soon as possible
            video.show();
        }

        @Override
        public void onAdClosed() {
            super.onAdClosed();
            Log.d(TAG, "Ad closed");
        }
    });

    return video;
}
 
Example 7
Source File: AdsManager.java    From FruitCatcher with Apache License 2.0 5 votes vote down vote up
public void showInterstitial(Activity activity, String interstitialAdUnitId) {
    final String interstitialAdTimeKey = "InterstitialAdTime";
    final SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(activity.getBaseContext());
    long adTime = preferences.getLong(interstitialAdTimeKey, 0);
    long now = (new Date()).getTime();
    long dayMs = 24*3600*1000;
    if (now - adTime < dayMs) {
        return;
    }
    interstitial = new InterstitialAd(activity);
    interstitial.setAdUnitId(interstitialAdUnitId);
    // Create ad request.
    AdRequest adRequest = new AdRequest.Builder().build();
    // Begin loading your interstitial.
    interstitial.loadAd(adRequest);
    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            try {
                if (interstitial.isLoaded()) {
                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putLong(interstitialAdTimeKey, (new Date()).getTime());
                    edit.commit();
                    interstitial.show();
                }
            }
            catch(Exception ex) {
            }
        }
    });
}
 
Example 8
Source File: MainActivity.java    From BusyBox with Apache License 2.0 5 votes vote down vote up
private InterstitialAd newInterstitialAd(String placementId, AdListener listener) {
    InterstitialAd interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdListener(listener);
    interstitialAd.setAdUnitId(placementId);
    interstitialAd.loadAd(getAdRequest());
    return interstitialAd;
}
 
Example 9
Source File: MainActivity.java    From BusyBox with Apache License 2.0 5 votes vote down vote up
private InterstitialAd newInterstitialAd(String placementId, AdListener listener) {
    InterstitialAd interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdListener(listener);
    interstitialAd.setAdUnitId(placementId);
    interstitialAd.loadAd(getAdRequest());
    return interstitialAd;
}
 
Example 10
Source File: GoogleAdActivity.java    From FastLib with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_ad);

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    // Create the InterstitialAd and set the adUnitId.
    interstitialAd = new InterstitialAd(this);
    // Defined in res/values/strings.xml
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            ToastUtil.show("onAdLoaded()");
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            ToastUtil.show("onAdFailedToLoad() with error code: " + errorCode);
        }

        @Override
        public void onAdClosed() {
            startGame();
        }
    });

    // Create the "retry" button, which tries to show an interstitial between game plays.
    retryButton = findViewById(R.id.retry_button);
    retryButton.setVisibility(View.INVISIBLE);
    retryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInterstitial();
        }
    });

    startGame();
}
 
Example 11
Source File: MyActivity.java    From googleads-mobile-android-examples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });

    // Create the InterstitialAd and set the adUnitId.
    interstitialAd = new InterstitialAd(this);
    // Defined in res/values/strings.xml
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            Toast.makeText(MyActivity.this,
                    "onAdFailedToLoad() with error code: " + errorCode,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdClosed() {
            startGame();
        }
    });

    // Create the "retry" button, which tries to show an interstitial between game plays.
    retryButton = findViewById(R.id.retry_button);
    retryButton.setVisibility(View.INVISIBLE);
    retryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInterstitial();
        }
    });

    startGame();
}
 
Example 12
Source File: AdMob.java    From GodotAds with Apache License 2.0 5 votes vote down vote up
public void createInterstitial() {
	String ad_unit_id = _config.optString("InterstitialAdId", "");

	if (ad_unit_id.length() <= 0) {
		Utils.d("GodotAds", "AdMob:Interstitial:UnitId:NotProvided");
		ad_unit_id = activity.getString(R.string.gads_interstitial_ad_unit_id);
	}

	mInterstitialAd = new InterstitialAd(activity);
	mInterstitialAd.setAdUnitId(ad_unit_id);
	mInterstitialAd.setAdListener(new AdListener() {
		@Override
		public void onAdLoaded() {
			Utils.d("GodotAds", "AdMob:Interstitial:OnAdLoaded");
			Utils.callScriptFunc("AdMob", "AdMob_Interstitial", "loaded");
		}

		@Override
		public void onAdFailedToLoad(int errorCode) {
			Utils.w("GodotAds", "AdMob:Interstitial:onAdFailedToLoad:" + errorCode);
			Utils.callScriptFunc("AdMob", "AdMob_Interstitial", "load_failed");
		}

		@Override
		public void onAdClosed() {
			Utils.w("GodotAds", "AdMob:Interstitial:onAdClosed");
			requestNewInterstitial();
		}

	});

	requestNewInterstitial();
}
 
Example 13
Source File: BaseAdsActivity.java    From twoh-android-material-design with MIT License 5 votes vote down vote up
protected void initInterstitial(){
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            loadInterstitial();
        }
    });

    loadInterstitial();
}
 
Example 14
Source File: MainActivity.java    From GooglePlayServiceLocationSupport with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mOldLocation = (TextView) findViewById(R.id.oldlocation);
    mNewLocation = (TextView) findViewById(R.id.newlocation);
    AdView mAdView = (AdView) findViewById(R.id.adView);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad));
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            startActivity(new Intent(MainActivity.this, FragmentLocationActivity.class));
        }
    });
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);

    adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    findViewById(R.id.btn_fragment).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd != null && mInterstitialAd.isLoaded())
                mInterstitialAd.show();
            else startActivity(new Intent(MainActivity.this, FragmentLocationActivity.class));
        }
    });
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
 
Example 15
Source File: MapActivity.java    From homeassist with Apache License 2.0 5 votes vote down vote up
private void setupAdSense() {
    Log.d("YouQi", "Map Adsense: " + ((FirebaseRemoteConfig.getInstance().getBoolean("adsense_map")) ? "Yes" : "No"));
    if (FirebaseRemoteConfig.getInstance().getBoolean("adsense_map")) {
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.banner_ad_unit_id_interstitial));
        mInterstitialAd.loadAd(CommonUtil.getAdRequest());
        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                mInterstitialAd.show();
            }
        });
    }
}
 
Example 16
Source File: MainActivity.java    From GDPR-Admob-Android with MIT License 5 votes vote down vote up
private void loadInterstitial() {
    final InterstitialAd interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    // You have to pass the AdRequest from ConsentSDK.getAdRequest(this) because it handle the right way to load the ad
    interstitialAd.loadAd(ConsentSDK.getAdRequest(this));
    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Show interstitial
            interstitialAd.show();
            super.onAdLoaded();
        }
    });
}
 
Example 17
Source File: AdMobPlugin.java    From cordova-admob-pro with MIT License 5 votes vote down vote up
@Override
protected void __destroyInterstitial(Object interstitial) {
  if(interstitial == null) return;

  if(interstitial instanceof InterstitialAd) {
    InterstitialAd ad = (InterstitialAd) interstitial;
    ad.setAdListener(null);
  }
}
 
Example 18
Source File: MainActivity.java    From MockSMS with Apache License 2.0 4 votes vote down vote up
private void startDaWork() {
    AdView mAdView = new AdView(MainActivity.this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId(All.ID_1_BAN);

    LinearLayout container = findViewById(R.id.adContainer);
    container.addView(mAdView, container.getChildCount());

    AdRequest adRequest = new AdRequest
            .Builder()
            .build();
    mAdView.loadAd(adRequest);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(All.ID_1_INTER);
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            Crashlytics.log("Ad ErrorCode" + errorCode);
        }

        @Override
        public void onAdOpened() {
            Extra.getInstance(getBaseContext()).setClickCount(0);
        }

        @Override
        public void onAdLeftApplication() {
            Extra.getInstance(getBaseContext()).setClickCount(0);
        }

        @Override
        public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

}
 
Example 19
Source File: MyActivity.java    From android-ads with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });

    // Create the InterstitialAd and set the adUnitId.
    interstitialAd = new InterstitialAd(this);
    // Defined in res/values/strings.xml
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            Toast.makeText(MyActivity.this,
                    "onAdFailedToLoad() with error code: " + errorCode,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdClosed() {
            startGame();
        }
    });

    // Create the "retry" button, which tries to show an interstitial between game plays.
    retryButton = findViewById(R.id.retry_button);
    retryButton.setVisibility(View.INVISIBLE);
    retryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInterstitial();
        }
    });

    startGame();
}
 
Example 20
Source File: MainActivity.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // [START_EXCLUDE]
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    mAdView = binding.adView;
    mLoadInterstitialButton = binding.loadInterstitialButton;
    View layout = binding.getRoot();
    // [END_EXCLUDE]
    setContentView(layout);
    checkIds();

    // Initialize the Google Mobile Ads SDK
    MobileAds.initialize(this, getString(R.string.admob_app_id));

    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    // [END load_banner_ad]

    // 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.

    // [START instantiate_interstitial_ad]
    // Create an InterstitialAd object. This same object can be re-used whenever you want to
    // show an interstitial.
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
    // [END instantiate_interstitial_ad]

    // [START create_interstitial_ad_listener]
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
            beginSecondActivity();
        }

        @Override
        public void onAdLoaded() {
            // Ad received, ready to display
            // [START_EXCLUDE]
            if (mLoadInterstitialButton != null) {
                mLoadInterstitialButton.setEnabled(true);
            }
            // [END_EXCLUDE]
        }

        @Override
        public void onAdFailedToLoad(int i) {
            // See https://goo.gl/sCZj0H for possible error codes.
            Log.w(TAG, "onAdFailedToLoad:" + i);
        }
    });
    // [END create_interstitial_ad_listener]

    // [START display_interstitial_ad]
    mLoadInterstitialButton = findViewById(R.id.loadInterstitialButton);
    mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                beginSecondActivity();
            }
        }
    });
    // [END display_interstitial_ad]

    // Disable button if an interstitial ad is not loaded yet.
    mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded());
}