Android 전면 광고
2
3
Activity에 전면 광고 구현
import io.adrop.ads.interstitial.AdropInterstitial
import io.adrop.ads.interstitial.AdropInterstitialListener
import io.adrop.ads.model.AdropErrorCode
class MainActivity : AppCompatActivity() {
private var interstitialAd: AdropInterstitial? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 전면 광고 로드
loadInterstitialAd()
// 버튼 클릭 시 광고 표시
findViewById<Button>(R.id.show_ad_button).setOnClickListener {
showInterstitialAd()
}
}
private fun loadInterstitialAd() {
interstitialAd = AdropInterstitial(
this,
AdropUnitId.INTERSTITIAL
).apply {
listener = interstitialListener
load()
}
}
private fun showInterstitialAd() {
if (interstitialAd?.isLoaded == true) {
interstitialAd?.show()
} else {
Log.d("AdropInterstitial", "광고가 아직 로드되지 않았습니다")
// 광고 재로드
loadInterstitialAd()
}
}
private val interstitialListener = object : AdropInterstitialListener {
override fun onAdReceived(ad: AdropInterstitial) {
Log.d("AdropInterstitial", "광고 로드 성공: ${ad.unitId}")
}
override fun onAdFailedToReceive(ad: AdropInterstitial, errorCode: AdropErrorCode) {
Log.e("AdropInterstitial", "광고 로드 실패: $errorCode")
}
override fun onAdOpened(ad: AdropInterstitial) {
Log.d("AdropInterstitial", "광고 노출됨")
}
override fun onAdClosed(ad: AdropInterstitial) {
Log.d("AdropInterstitial", "광고 닫힘")
// 새 광고 미리 로드
loadInterstitialAd()
}
override fun onAdClicked(ad: AdropInterstitial) {
Log.d("AdropInterstitial", "광고 클릭됨")
}
override fun onAdImpression(ad: AdropInterstitial) {
Log.d("AdropInterstitial", "광고 노출 확인됨")
}
}
}Last updated