본문 바로가기
프로그래밍

안드로이드 - 전면광고 + 배너광고

by 삼굽살 2020. 12. 29.
반응형

기록용


private AdView mAdView; //배너광고
private InterstitialAd mInterstitialAd; //전면광고

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 광고 초기화
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

//배너
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

//전면
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}

@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}

@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}

@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}

@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}

@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});

}

}

댓글