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
@@ -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()
|
||||
|
||||
@@ -158,12 +158,10 @@
|
||||
|
||||
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/adView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="0dp"
|
||||
ads:adSize="SMART_BANNER"
|
||||
ads:adUnitId="@string/admob_bannerid" />
|
||||
android:layout_marginBottom="0dp" />
|
||||
|
||||
</RelativeLayout>
|
||||
Reference in new issue
Block a user