android – 如何避免在配置更改和启动活动时重新加载Admob广告?

前端之家收集整理的这篇文章主要介绍了android – 如何避免在配置更改和启动活动时重新加载Admob广告?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用横幅样本 here,我注意到如果我改变方向,广告就会消失,然后重新加载(需要时间),所以每当我改变方向时,都会有时间显示任何内容.

我知道可以通过清单为活动设置configChanges(实际上它可以正常工作),但这不是一般的建议.它有多个缺陷:

>在我的情况下,例如,更改方向让工具栏保持在相同的高度,这是错误的,因为它应该在横向上与在纵向上不同.
>转到另一个活动Admob视图仍会重新加载,即使之前已加载.
>当从纵向转向横向时,反之亦然,广告无法很好地适应新的方向.

在每次配置更改时,还可以采取哪些措施来避免重新加载广告?

我希望Admob一次加载广告(其余部分在后台加载新广告),然后展示,并避免在我更改方向时重新加载.每次到达时,只显示当前配置(例如方向)的最佳状态.

同样适用于使用Admob广告的新活动.如果之前加载,则无需重新加载.

编辑:我已经给了这个奖励,因为Google / Firebase支持通过电子邮件告诉我,实际上可以缓存广告:

The Ads SDK won’t cache ads for you,however you can implement an
‘AdManager’ style class,most likely a singleton or an instance
attached to your application instance,which can manage caching for
you.

As you recognize it’s an Android pattern to destroy and recreate
activities on orientation,instance state is usually saved and
restored via a bundle,but this is not applicable to views.

If you wish to optimize the reload time of the banner view in this
scenario,I would recommend making your ad request via an AdManager
class,and the AdManager can then apply some logic to cache ads such
as your banner view for a certain amount of time,and return the
prevIoUs banner if it is still valid for the new request (which you
would make on orientation change).

而且:

This idea of caching ads is not implemented by the SDK,as it is
difficult to determine the best caching logic to apply in all cases,
thus it is left to the developer to implement caching that suits their
use case,if desired.

Possibly having a banner ad decoupled from the ‘AdView’ class would
make for more appropriate cacheable objects,however this is not
currently the case.

Therefore as it is,yes we suggest that in order to prefetch / cache a
banner ad,you need to store the ‘AdView’. Instead of using an
AdManager however,another supported and indeed recommended approach
would be to use a viewmodel,see
07001
and
07002.
These are designed for exactly this kind of use case.

如果确实有可能,我想知道如何做到这一点.请成功执行此操作的人员,在代码显示可以执行的操作.

解决方法

我认为使用智能横幅将是一个不错的选择,因为它们适用于任何方向
要使用智能横幅,请为广告尺寸指定常量SMART_BANNER:
AdView adView = new AdView(this);

adView.setAdSize(AdSize.SMART_BANNER);

还检查一下:
https://firebase.google.com/docs/admob/android/banner#smart_banners

猜你在找的Android相关文章