我正在使用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
}
}
如何使广告加载更快,是否需要设置或设置特殊帐户设置?