gpt4 book ai didi

kotlin - 如何在 android studio 中实现 Admob Interstitial Ad - Kotlin

转载 作者:行者123 更新时间:2023-12-04 13:27:01 28 4
gpt4 key购买 nike

我是初学者,我需要帮助在 android studio 中使用 Kotlin 实现 Admob Interstitial Ad。对不起,我的问题,如果你能一步一步地告诉我,那么我就可以学习这个过程,并且每隔一段时间自己做一次。提前致谢。

package com.example.myapplication

import android.content.ContentValues.TAG
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback

class MainActivity : AppCompatActivity() {

private var mInterstitialAd: InterstitialAd? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val one_Page: TextView = findViewById(R.id.one_Page)
val three_Page: TextView = findViewById(R.id.three_Page)



one_Page.setOnClickListener {
val intent = Intent(this, PageActivity2::class.java)
// start your next activity
startActivity(intent)

}
three_Page.setOnClickListener {
val intent = Intent(this, PageActivity2::class.java)
// start your next activity
startActivity(intent)
}


}

}

最佳答案

我想也许您错过了几个步骤才能正确完成,请按照以下步骤操作:
注意:我将使用示例 ID 加载演示广告,您需要确保它有效然后考虑用真实 ID 替换示例 ID
1:设置 Admob 依赖项:

implementation 'com.google.android.gms:play-services-ads:20.1.0'
2:在 <application>中的AndroidManifest.xml中添加代码片段标签
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<!-- Replace sample id if you have a real id from AdMob project - pattern ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
3:初始化 Admob 并加载一个广告:
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
class MainActivity : AppCompatActivity() {
private var mInterstitialAd: InterstitialAd? = null
private final var TAG = 'MainActivity'
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize Admob
MobileAds.initialize(this) {}
var adRequest = AdRequest.Builder().build()
// ca-app-pub-3940256099942544/1033173712 is a sample interstial id.
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.message)
mInterstitialAd = null
}

override fun onAdLoaded(interstitialAd: InterstitialAd) {
Log.d(TAG, 'Ad was loaded.')
mInterstitialAd = interstitialAd
}
})
}
}
4:在 InterstitialAd.load下方添加FullScreenContentCallback :
mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d(TAG, 'Ad was dismissed.')
}

override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
Log.d(TAG, 'Ad failed to show.')
}

override fun onAdShowedFullScreenContent() {
Log.d(TAG, 'Ad showed fullscreen content.')
mInterstitialAd = null;
}
}
6:将 showFullAd 添加到 MainActivity 并调用 showFullAd在您想要展示的事件中
fun showFullAd(){
if (mInterstitialAd != null) {
mInterstitialAd?.show(this)
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.")
}
}
查看更多:
  • Get Started with Admob
  • Implement Interstitial ads
  • 关于kotlin - 如何在 android studio 中实现 Admob Interstitial Ad - Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67872537/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com