From 78f0b0575cd847eb3606720f3221d5a09b4035e0 Mon Sep 17 00:00:00 2001 From: Kevin Puertas Date: Tue, 20 Oct 2026 13:24:04 +0000 Subject: [PATCH] AdMob: UMP consent (EEA), MobileAds.initialize en background, banner adaptativo cargado (estaba muerto en el layout), rewarded con recarga tras consumo (era de un solo uso) --- app/build.gradle | 1 + .../st/frasesdeanime/MainActivity.kt | 83 +++++++++++++++---- app/src/main/res/layout/activity_main.xml | 6 +- app/versioncode.count | 4 +- gradlew | 0 5 files changed, 73 insertions(+), 21 deletions(-) mode change 100644 => 100755 gradlew diff --git a/app/build.gradle b/app/build.gradle index 3495885..0036f2a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -71,4 +71,5 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.7.0' implementation 'com.google.android.gms:play-services-ads:25.5.0' + implementation 'com.google.android.ump:user-messaging-platform:4.0.0' } diff --git a/app/src/main/kotlin/com/jkanetwork/st/frasesdeanime/MainActivity.kt b/app/src/main/kotlin/com/jkanetwork/st/frasesdeanime/MainActivity.kt index ca0b862..67d41ba 100644 --- a/app/src/main/kotlin/com/jkanetwork/st/frasesdeanime/MainActivity.kt +++ b/app/src/main/kotlin/com/jkanetwork/st/frasesdeanime/MainActivity.kt @@ -20,8 +20,12 @@ import androidx.core.content.ContextCompat /* Ads */ import com.google.android.gms.ads.AdRequest +import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.MobileAds +import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.rewarded.RewardedAd; +import com.google.android.ump.ConsentRequestParameters +import com.google.android.ump.UserMessagingPlatform import android.text.method.ScrollingMovementMethod @@ -62,21 +66,8 @@ class MainActivity : AppCompatActivity() { db = SQLiteHelper(applicationContext) - /* Ads */ - // Initialize the Mobile Ads SDK. - MobileAds.initialize(this) { } - - // Cargar el anuncio recompensado con el ID de UNIDAD (videoid), no el de app - val adRequest = AdRequest.Builder().build() - RewardedAd.load(this, this.getString(R.string.admob_videoid), adRequest, object : RewardedAdLoadCallback() { - override fun onAdFailedToLoad(adError: LoadAdError) { - rewardedAd = null - } - - override fun onAdLoaded(ad: RewardedAd) { - rewardedAd = ad - } - }) + /* Ads: UMP consent (obligatorio EEA) → init en background → banner + rewarded */ + setUpConsentAndAds() /* Check if first time or not, for welcoming user */ @@ -137,6 +128,13 @@ class MainActivity : AppCompatActivity() { if (IDFraseS > 0) { rewardedAd?.let { ad -> + /* Recargar tras cerrar: los RewardedAd son de un solo uso */ + ad.fullScreenContentCallback = object : FullScreenContentCallback() { + override fun onAdDismissedFullScreenContent() { + rewardedAd = null + loadRewardedAd() + } + } ad.show(this, OnUserEarnedRewardListener { rewardItem -> // Handle the reward. ANUNCIO VISTO CORRECTAMENTE. val gato = ImageView(this@MainActivity) @@ -214,6 +212,61 @@ class MainActivity : AppCompatActivity() { } + /* ------------------------------------------------------------------ */ + /* Ads: UMP consent → init background → banner adaptativo + rewarded */ + /* ------------------------------------------------------------------ */ + + /** Consentimiento UMP (obligatorio EEA para servir anuncios) y tras él, init + carga. */ + private fun setUpConsentAndAds() { + val params = ConsentRequestParameters.Builder().build() + val consentInfo = UserMessagingPlatform.getConsentInformation(this) + consentInfo.requestConsentInfoUpdate( + this, + params, + { + UserMessagingPlatform.loadAndShowConsentFormIfRequired(this) { initAdsAfterConsent() } + }, + { initAdsAfterConsent() } // sin info de consent (offline, etc.): seguir igual + ) + } + + /** MobileAds.initialize en background y, al completar, banner + rewarded. */ + private fun initAdsAfterConsent() { + Thread { + MobileAds.initialize(applicationContext) { + runOnUiThread { + loadBannerAd() + loadRewardedAd() + } + } + }.start() + } + + /** Banner adaptativo anclado abajo (sustituye al SMART_BANNER deprecated). */ + private fun loadBannerAd() { + binding.adView.adUnitId = getString(R.string.admob_bannerid) + // AdSize trabaja en dp, no en píxeles físicos + val adWidthDp = (resources.displayMetrics.widthPixels / resources.displayMetrics.density).toInt() + binding.adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( + this, adWidthDp)) + binding.adView.loadAd(AdRequest.Builder().build()) + } + + /** Carga el anuncio recompensado con el ID de UNIDAD (videoid), no el de app. */ + private fun loadRewardedAd() { + // ID de unidad correcto: admob_videoid (admob_id es el de aplicación) + val adRequest = AdRequest.Builder().build() + RewardedAd.load(this, this.getString(R.string.admob_videoid), adRequest, object : RewardedAdLoadCallback() { + override fun onAdFailedToLoad(adError: LoadAdError) { + rewardedAd = null + } + + override fun onAdLoaded(ad: RewardedAd) { + rewardedAd = ad + } + }) + } + /** At onResume, called after onCreate and in resume, load ad things and refresh calendar data */ override fun onResume() { super.onResume() diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4213570..5bd1149 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -158,12 +158,10 @@ + android:layout_marginBottom="0dp" /> \ No newline at end of file diff --git a/app/versioncode.count b/app/versioncode.count index 95bce30..51900ae 100644 --- a/app/versioncode.count +++ b/app/versioncode.count @@ -1,2 +1,2 @@ -#Sun Oct 18 20:45:57 CEST 2026 -VERSION_CODE=1375 +#Tue Oct 20 13:23:19 UTC 2026 +VERSION_CODE=1382 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755