首页 > 解决方案 > Admob 查询广告很慢

问题描述

我正在使用 Admob 在回收站视图中查询原生广告,但是在加载广告时,它会持续 7 秒。

我用来获取广告的代码如下:

 internal suspend fun getAdsFromAdMob(): Result<List<GoogleAd>> {
        val builder = AdLoader.Builder(App.sContext, BuildConfig.ADMOB_UNIT_ID)

        val nativeAds = ArrayList<GoogleAd>(Constants.ADS_QUERY_LIMIT)

        var adLoader: AdLoader? = null

        return suspendCoroutine { continuation ->
            adLoader = builder.forUnifiedNativeAd { unifiedNativeAd ->
                // A native ad has been loaded successfully, add it into the mNativeAds array list
                nativeAds.add(GoogleAd(unifiedNativeAd))
                if (nativeAds.size == Constants.ADS_QUERY_LIMIT || !adLoader!!.isLoading) { // adLoader won't be null by now
                    continuation.resume(Result.Success(nativeAds))
                    adLoader = null // Set the adLoader to null for it to be garbage collected
                }

            }.withAdListener(
                object : AdListener() {
                    override fun onAdFailedToLoad(errorCode: Int) {
                        // Send the error to Crashlytics
                        NullPointerException("Error while loading ads with error code: $errorCode").sendErrorToCrashlytics()

                        continuation.resume(Result.Error(NullPointerException("Error while loading ads with error code: $errorCode")))
                    }
                }).build()

            adLoader!!.loadAds(AdRequest.Builder().build(), Constants.ADS_QUERY_LIMIT) // adLoader won't be null by now
        }
    }

我怎样才能使广告加载更快,是否有我必须设置的设置或特殊帐户设置?

标签: androidadmobads

解决方案


推荐阅读