Where should i call MobileAds.initialize()?

后端 未结 3 1800
失恋的感觉
失恋的感觉 2021-02-20 02:49

I have read https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdk

I need to initialize MobileAds using Code A in order to display Ad

相关标签:
3条回答
  • 2021-02-20 03:11

    From the docs of MobileAds.initialize():

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

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

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

    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>
    

    And regarding your question:

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

    MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
    

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

    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.

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

    0 讨论(0)
  • 2021-02-20 03:20

    MobileAds.initialize(this, "YOUR-APP-ID") has deprecated. Use the below code instead.

    import android.app.Application
    import com.google.android.gms.ads.MobileAds
    
    class MyApp: Application() {
    
       override fun onCreate() {
           MobileAds.initialize(applicationContext)
           super.onCreate()
       }
    
    }
    
    0 讨论(0)
  • 2021-02-20 03:21

    Initializing MobileAds in Aplication context will cause error with InMobi mediation adapter

    "Adapter Initialization status: Not Ready"
    

    Tested with AdMob Test Suite.

    Solution is to initialize MobileAds in Activity context.

    0 讨论(0)
提交回复
热议问题