尝试使用 Kotlin 将激励广告正确实施到 Android Studio
Trying to implement Rewarded Ad to Android Studio correctly with Kotlin
大家好,我有一个随机抽取乐透球的应用程序。我有一个用于增加绑定到激励广告的令牌计数的按钮。但我无法使其正常运行。这是我的代码:
package com.zumatrahia.kazandiranlototahmini
import android.content.Context
import android.content.pm.ActivityInfo
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
class SayisalActivity : AppCompatActivity() {
lateinit var Ball01: TextView
lateinit var Ball02: TextView
lateinit var Ball03: TextView
lateinit var Ball04: TextView
lateinit var Ball05: TextView
lateinit var Ball06: TextView
lateinit var mediaPlayer: MediaPlayer
lateinit var tokenText: TextView
var tokenCount: Int = 0
private var mRewardedAd: RewardedAd? = null
private final var TAG = "SayisalActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sayisal)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
Ball01 = findViewById(R.id.Ball01)
Ball02 = findViewById(R.id.Ball02)
Ball03 = findViewById(R.id.Ball03)
Ball04 = findViewById(R.id.Ball04)
Ball05 = findViewById(R.id.Ball05)
Ball06 = findViewById(R.id.Ball06)
mediaPlayer = MediaPlayer.create(this, R.raw.success_sound)
MobileAds.initialize(this) {loadRewardedAd()}
val spinner: Spinner = findViewById(R.id.spinner)
ArrayAdapter.createFromResource(
this, R.array.lotto_choices, R.layout.spinner_list
).also { adapter ->
adapter.setDropDownViewResource(R.layout.spinner_list)
spinner.adapter = adapter
}
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
tokenCount = sharedPreferences.getInt("INT_KEY", 5)
tokenText = findViewById(R.id.token_text)
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
}
private fun loadRewardedAd() {
var adRequest = AdRequest.Builder().build()
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, "Ad wasn't loaded")
mRewardedAd = null
loadRewardedAd()
}
override fun onAdLoaded(rewardedAd: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
mRewardedAd = rewardedAd
mRewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d(TAG, "Ad was dismissed.")
loadRewardedAd()
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
Log.d(TAG, "Ad failed to show.")
loadRewardedAd()
}
override fun onAdShowedFullScreenContent() {
Log.d(TAG, "Ad showed fullscreen content.")
mRewardedAd = null
}
}
}
})
}
private fun showRewardedAd(){
if (mRewardedAd != null) {
mRewardedAd?.show(this, OnUserEarnedRewardListener() {
fun onUserEarnedReward(rewardItem: RewardItem) {
var rewardAmount = rewardItem.amount
var rewardType = rewardItem.type
Log.d(TAG, "User earned the reward.")
tokenCountUp()
}
})
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
loadRewardedAd()
}
}
fun playButtonPressed(view: View) {
mediaPlayer.start()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 50).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 100).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 150).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 200).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 300).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 400).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 500).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 800).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1100).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1600).textChange()
tokenCountDown()
}
fun rewardButtonPressed(view: View) {
showRewardedAd()
}
fun tokenCountDown() {
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
tokenCount -= 1
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
putInt("INT_KEY", tokenCount)
}.apply()
}
fun tokenCountUp() {
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
tokenCount += 1
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
putInt("INT_KEY", tokenCount)
}.apply()
}
}
问题是,当我点击按钮并在广告中打开激励视频广告时,它会倒计时奖励并显示“用户已获得奖励”,但 Log.d(TAG,“用户获得了奖励。”)没有实行。当然,tokenCountUp() 不会实现 either.Only 广告已加载并且广告显示全屏内容进入日志。
但是在一个教程中,我跟随用户获得了奖励标签来到日志屏幕。如果我将 tokenCountUp() 置于覆盖 fun onAdShowedFullScreenContent() 或 showRewardedAd(){
if (mRewardedAd != null) { 它有效,但在用户实际观看广告之前它会计数。
我哪里做错了?
我将 showRewardedAd 功能内容更改为此,现在可以使用了
if (mRewardedAd != null) {
mRewardedAd!!.show(this) { rewardItem -> // Handle the reward.
Log.d(TAG, "The user earned the reward.")
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
tokenCountUp()
}
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
loadRewardedAd()
}
大家好,我有一个随机抽取乐透球的应用程序。我有一个用于增加绑定到激励广告的令牌计数的按钮。但我无法使其正常运行。这是我的代码:
package com.zumatrahia.kazandiranlototahmini
import android.content.Context
import android.content.pm.ActivityInfo
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
class SayisalActivity : AppCompatActivity() {
lateinit var Ball01: TextView
lateinit var Ball02: TextView
lateinit var Ball03: TextView
lateinit var Ball04: TextView
lateinit var Ball05: TextView
lateinit var Ball06: TextView
lateinit var mediaPlayer: MediaPlayer
lateinit var tokenText: TextView
var tokenCount: Int = 0
private var mRewardedAd: RewardedAd? = null
private final var TAG = "SayisalActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sayisal)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
Ball01 = findViewById(R.id.Ball01)
Ball02 = findViewById(R.id.Ball02)
Ball03 = findViewById(R.id.Ball03)
Ball04 = findViewById(R.id.Ball04)
Ball05 = findViewById(R.id.Ball05)
Ball06 = findViewById(R.id.Ball06)
mediaPlayer = MediaPlayer.create(this, R.raw.success_sound)
MobileAds.initialize(this) {loadRewardedAd()}
val spinner: Spinner = findViewById(R.id.spinner)
ArrayAdapter.createFromResource(
this, R.array.lotto_choices, R.layout.spinner_list
).also { adapter ->
adapter.setDropDownViewResource(R.layout.spinner_list)
spinner.adapter = adapter
}
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
tokenCount = sharedPreferences.getInt("INT_KEY", 5)
tokenText = findViewById(R.id.token_text)
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
}
private fun loadRewardedAd() {
var adRequest = AdRequest.Builder().build()
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, "Ad wasn't loaded")
mRewardedAd = null
loadRewardedAd()
}
override fun onAdLoaded(rewardedAd: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
mRewardedAd = rewardedAd
mRewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d(TAG, "Ad was dismissed.")
loadRewardedAd()
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
Log.d(TAG, "Ad failed to show.")
loadRewardedAd()
}
override fun onAdShowedFullScreenContent() {
Log.d(TAG, "Ad showed fullscreen content.")
mRewardedAd = null
}
}
}
})
}
private fun showRewardedAd(){
if (mRewardedAd != null) {
mRewardedAd?.show(this, OnUserEarnedRewardListener() {
fun onUserEarnedReward(rewardItem: RewardItem) {
var rewardAmount = rewardItem.amount
var rewardType = rewardItem.type
Log.d(TAG, "User earned the reward.")
tokenCountUp()
}
})
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
loadRewardedAd()
}
}
fun playButtonPressed(view: View) {
mediaPlayer.start()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 50).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 100).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 150).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 200).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 300).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 400).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 500).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 800).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1100).textChange()
BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1600).textChange()
tokenCountDown()
}
fun rewardButtonPressed(view: View) {
showRewardedAd()
}
fun tokenCountDown() {
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
tokenCount -= 1
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
putInt("INT_KEY", tokenCount)
}.apply()
}
fun tokenCountUp() {
val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
tokenCount += 1
tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
putInt("INT_KEY", tokenCount)
}.apply()
}
}
问题是,当我点击按钮并在广告中打开激励视频广告时,它会倒计时奖励并显示“用户已获得奖励”,但 Log.d(TAG,“用户获得了奖励。”)没有实行。当然,tokenCountUp() 不会实现 either.Only 广告已加载并且广告显示全屏内容进入日志。 但是在一个教程中,我跟随用户获得了奖励标签来到日志屏幕。如果我将 tokenCountUp() 置于覆盖 fun onAdShowedFullScreenContent() 或 showRewardedAd(){ if (mRewardedAd != null) { 它有效,但在用户实际观看广告之前它会计数。 我哪里做错了?
我将 showRewardedAd 功能内容更改为此,现在可以使用了
if (mRewardedAd != null) {
mRewardedAd!!.show(this) { rewardItem -> // Handle the reward.
Log.d(TAG, "The user earned the reward.")
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
tokenCountUp()
}
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
loadRewardedAd()
}