我有一个用Kotlin写的android项目。我可以使用kotlin添加admob广告吗?
答案 0 :(得分:0)
是您可以通过在类中初始化以下变量来实现此目的
internal lateinit var mInterstitialAd: InterstitialAd
internal lateinit var adRequest : AdRequest
使用以下代码
在覆盖趣味onCreate中开始广告请求mInterstitialAd = InterstitialAd(this);
mInterstitialAd.adUnitId = " YOUR INTERSTITIAL ID";
adRequest = AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.adListener = object : AdListener() {
@Override public override fun onAdLoaded() {
requestNewInterstitial()
}
};
访问此链接,了解使用Kotlin进行完整的admob使用和集成:Tutorial
答案 1 :(得分:0)
lateinit var interstitialAd: InterstitialAd
lateinit var adRequest: AdRequest
interstitialAd = InterstitialAd(this)
interstitialAd.adUnitId = getString(R.string.admob_interstitial)
adRequest = AdRequest.Builder().build()
interstitialAd.loadAd(adRequest)
interstitialAd.adListener = object : AdListener() {
override fun onAdLoaded() {
if (interstitialAd.isLoaded) {
interstitialAd.show()
}
}
}