奖励广告在 Android 中无法正常运行
Posted
技术标签:
【中文标题】奖励广告在 Android 中无法正常运行【英文标题】:Rewarded ads not working properly in Android 【发布时间】:2021-12-21 01:43:30 【问题描述】:这是我的 MainActivity -
class MainActivity() : AppCompatActivity()
private var btn1: Button? = null
private var mRewardedAd: RewardedAd? = null
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1 = findViewById<View>(R.id.button1) as Button
btn1?.setOnClickListener
loadAd()
showAd()
if (mRewardedAd != null)
val intent: Intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
Toast.makeText(this, " Ad completed", Toast.LENGTH_SHORT).show()
private fun showAd()
if (mRewardedAd != null)
mRewardedAd?.show(this, OnUserEarnedRewardListener()
fun onUserEarnedReward(rewardItem: RewardItem)
var rewardAmount = rewardItem.getAmount()
var rewardType = rewardItem.getType()
Log.d(TAG, "User earned the reward.")
Toast.makeText(this, "User earned the reward.", Toast.LENGTH_SHORT).show()
)
else
Log.d(TAG, "The rewarded ad wasn't ready yet.")
Toast.makeText(this, "The rewarded ad wasn't ready yet.", Toast.LENGTH_SHORT).show()
private fun loadAd()
var adRequest = AdRequest.Builder().build()
RewardedAd.load(
this,
"ca-app-pub-3940256099942544/5224354917",
adRequest,
object : RewardedAdLoadCallback()
override fun onAdFailedToLoad(adError: LoadAdError)
Log.d(TAG, adError?.message)
mRewardedAd = null
override fun onAdLoaded(rewardedAd: RewardedAd)
Log.d(TAG, "Ad was loaded.")
mRewardedAd = rewardedAd
)
MainActivity.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_
android:layout_
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_marginTop="224dp"
android:backgroundTint="#9C27B0"
android:text="Button main Screen"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.589"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
奖励广告并未加载所有内容。有时,当我按回来时,广告就来了。不知道它是如何工作的。我想当我按下 MainActivity 中的按钮时,它会在进入下一个 Activity 后显示奖励广告。请帮我解决这个问题。
我正在使用 Android Intent 转到 nextActivity。有时它会转到 nextActivity 而不显示广告。奖励广告并非每次都加载。现在我正在检查文字广告。
【问题讨论】:
【参考方案1】:当用户点击按钮时,您同时调用 loadAd()
和 showAd()
。
您需要在内存中有一个广告才能显示它。
您必须在初始化您的 MobileAds
SDK 后立即调用 loadAd()
,因此您的内存中始终有一个加载的广告,您可以稍后在单击按钮或任何其他事件时显示该广告。
从btn1?.setOnClickListener
中移除loadAd()
调用并将其移动到onCreate()
,您还必须初始化MobileAds
SDK
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MobileAds.initialize(this)
loadAd()
//here goes your remaining code
在showAd()
方法中,设置fullScreenContentCallback
,在mRewarededAd
的null
检查之后,在用户观看奖励广告后加载新广告。
mRewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback()
override fun onAdDismissedFullScreenContent()
Log.d(TAG, "Ad was dismissed.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
mRewardedAd = null
// Reload rewarded ad
loadAd()
override fun onAdFailedToShowFullScreenContent(adError: AdError?)
Log.d(TAG, "Ad failed to show.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
mRewardedAd = null
override fun onAdShowedFullScreenContent()
Log.d(TAG, "Ad showed fullscreen content.")
// Called when ad is dismissed.
【讨论】:
以上是关于奖励广告在 Android 中无法正常运行的主要内容,如果未能解决你的问题,请参考以下文章
如何在奖励广告中添加 onAdClick 回调? - AdMob - 安卓