我应该在哪里调用MobileAds.initialize()? [英] Where should i call MobileAds.initialize()?

查看:547
本文介绍了我应该在哪里调用MobileAds.initialize()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要使用代码A初始化MobileAds才能显示AdMob AD.

I need to initialize MobileAds using Code A in order to display AdMob AD.

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

I have some activities which need to display ADs, do I need to add code A in my all activities?

还有,即使我删除

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")
    }
    ...
}

推荐答案

来自

应尽早调用此方法,并且每次仅调用一次 应用启动.

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

执行此操作的正确方法是在Application类的onCreate()方法中调用它.

The proper way to do this would be to call it in the onCreate() method of the Application class.

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

If you don't have an Application class, simply create one, something like this:

class YourApp: Application() {

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

您必须通过设置application标记的android:name属性在 AndroidManifest.xml 中引用此类:

You have to reference this class in AndroidManifest.xml, by setting the android:name attribute of the application tag:

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

    <!-- ... -->

</application>

关于您的问题:

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

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

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")

来自 Veer的

引用移动广告SDK团队的Arjun Busani :

Quote from Veer Arjun Busani of the Mobile Ads SDK team:

Mobile Ads SDK需要花费几毫秒的时间进行初始化,因此我们 现在,您甚至在调用自己的 第一个广告.一旦完成,将不会增加任何加载时间 对于您的第一个请求.如果您不打电话给您,那么您的第一个 首先,AdRequest需要花费几毫秒的时间 初始化自己.

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将隐式调用它.

So basically if you do not call MobileAds.initialize(), then the first AdRequest will call it implicitly.

这篇关于我应该在哪里调用MobileAds.initialize()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆