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)
This commit is contained in:
1 parent
af2c940a50
commit
78f0b0575c
5 files changed
+73
-21
No files matched your search
@@ -71,4 +71,5 @@ dependencies {
|
|||||||
implementation 'androidx.appcompat:appcompat:1.7.0'
|
implementation 'androidx.appcompat:appcompat:1.7.0'
|
||||||
|
|
||||||
implementation 'com.google.android.gms:play-services-ads:25.5.0'
|
implementation 'com.google.android.gms:play-services-ads:25.5.0'
|
||||||
|
implementation 'com.google.android.ump:user-messaging-platform:4.0.0'
|
||||||
}
|
}
|
||||||
@@ -20,8 +20,12 @@ import androidx.core.content.ContextCompat
|
|||||||
|
|
||||||
/* Ads */
|
/* Ads */
|
||||||
import com.google.android.gms.ads.AdRequest
|
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.MobileAds
|
||||||
|
import com.google.android.gms.ads.FullScreenContentCallback
|
||||||
import com.google.android.gms.ads.rewarded.RewardedAd;
|
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
|
import android.text.method.ScrollingMovementMethod
|
||||||
@@ -62,21 +66,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
db = SQLiteHelper(applicationContext)
|
db = SQLiteHelper(applicationContext)
|
||||||
|
|
||||||
|
|
||||||
/* Ads */
|
/* Ads: UMP consent (obligatorio EEA) → init en background → banner + rewarded */
|
||||||
// Initialize the Mobile Ads SDK.
|
setUpConsentAndAds()
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
/* Check if first time or not, for welcoming user */
|
/* Check if first time or not, for welcoming user */
|
||||||
@@ -137,6 +128,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
if (IDFraseS > 0) {
|
if (IDFraseS > 0) {
|
||||||
rewardedAd?.let { ad ->
|
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 ->
|
ad.show(this, OnUserEarnedRewardListener { rewardItem ->
|
||||||
// Handle the reward. ANUNCIO VISTO CORRECTAMENTE.
|
// Handle the reward. ANUNCIO VISTO CORRECTAMENTE.
|
||||||
val gato = ImageView(this@MainActivity)
|
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 */
|
/** At onResume, called after onCreate and in resume, load ad things and refresh calendar data */
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|||||||
@@ -158,12 +158,10 @@
|
|||||||
|
|
||||||
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
|
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
|
||||||
android:id="@+id/adView"
|
android:id="@+id/adView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_alignParentBottom="true"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:layout_marginBottom="0dp"
|
android:layout_marginBottom="0dp" />
|
||||||
ads:adSize="SMART_BANNER"
|
|
||||||
ads:adUnitId="@string/admob_bannerid" />
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
#Sun Oct 18 20:45:57 CEST 2026
|
#Tue Oct 20 13:23:19 UTC 2026
|
||||||
VERSION_CODE=1375
|
VERSION_CODE=1382
|
||||||
Reference in new issue
Block a user