package
org.geeksforgeeks.geeksforgeeks;
import
android.os.Bundle;
import
androidx.appcompat.app.AppCompatActivity;
import
com.google.android.gms.ads.AdListener;
import
com.google.android.gms.ads.AdRequest;
import
com.google.android.gms.ads.InterstitialAd;
import
com.google.android.gms.ads.MobileAds;
public
class
MainActivity
extends
AppCompatActivity {
private
InterstitialAd interstitial;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(
this
, getString(R.string.admob_app_id));
AdRequest adIRequest =
new
AdRequest.Builder().build();
interstitial =
new
InterstitialAd(MainActivity.
this
);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adIRequest);
interstitial.setAdListener(
new
AdListener() {
@Override
public
void
onAdLoaded() {
displayInterstitial();
}
});
}
public
void
displayInterstitial() {
if
(interstitial.isLoaded()) {
interstitial.show();
}
}
}