gpt4 book ai didi

android - 我应该在哪里调用 MobileAds.initialize()?

转载 作者:IT老高 更新时间:2023-10-28 13:44:13 24 4
gpt4 key购买 nike

我已阅读 https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdk

我需要使用代码 A 初始化 MobileAds 以显示 AdMob AD。

我有一些 Activity 需要展示广告,是否需要在所有 Activity 中添加代码 A?

还有,为什么即使我删除了 AdMob 广告也能正确显示

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")

代码 A

import com.google.android.gms.ads.MobileAds;

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

// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
}
...
}

最佳答案

来自 docs MobileAds.initialize():

This method should be called as early as possible, and only once per application launch.

正确的方法是在 Application 类的 onCreate() 方法中调用它。

如果您没有 Application 类,只需创建一个,如下所示:

class YourApp: Application() {

override fun onCreate() {
super.onCreate()
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
}
}

你必须在 AndroidManifest.xml 中引用这个类,方法是设置 application 标签的 android:name 属性:

<application
android:name=".YourApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<!-- ... -->

</application>

关于你的问题:

why can the AdMob Ad be displayed correctly even if I remove

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")

Quote来自移动广告 SDK 团队的 Veer Arjun Busani:

The Mobile Ads SDK take a few milliseconds to initialize itself and we now have provided this method to call it way before you even call your first ad. Once that is done, there would not be any added load time for your first request. If you do not call this, then your very first AdRequest would take a few milliseconds more as it first needs to initialize itself.

所以基本上如果你不调用MobileAds.initialize(),那么第一个AdRequest会隐式调用它。

关于android - 我应该在哪里调用 MobileAds.initialize()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49270255/

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