Interstetial Ads

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.neco_desarrollo.earnapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var interAd: InterstitialAd? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initAdMob()
binding.button.setOnClickListener {
showInterAd()
}
}

override fun onResume() {
super.onResume()
binding.adView.resume()
loadInterAd()
}

override fun onPause() {
super.onPause()
binding.adView.pause()
}

override fun onDestroy() {
super.onDestroy()
binding.adView.destroy()
}

private fun initAdMob(){
MobileAds.initialize(this)
val adRequest = AdRequest.Builder().build()
binding.adView.loadAd(adRequest)
}

private fun loadInterAd(){
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(this,
"ca-app-pub-3940256099942544/1033173712", adRequest,
object : InterstitialAdLoadCallback(){
override fun onAdFailedToLoad(p0: LoadAdError) {
interAd = null
}

override fun onAdLoaded(ad: InterstitialAd) {
interAd = ad
}
})
}

private fun showInterAd(){
if(interAd != null){
interAd?.fullScreenContentCallback =
object : FullScreenContentCallback(){
override fun onAdDismissedFullScreenContent() {
showContent()
interAd = null
loadInterAd()
}

override fun onAdFailedToShowFullScreenContent(p0: AdError) {
showContent()
interAd = null
loadInterAd()
}

override fun onAdShowedFullScreenContent() {
interAd = null
loadInterAd()
}
}

interAd?.show(this)
} else {
showContent()
}
}

private fun showContent(){
Toast.makeText(this, "Запуск контента", Toast.LENGTH_LONG).show()
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *