横幅和 MREC
以下各节将向您介绍如何加载、显示和隐藏横幅或 MREC 广告。
加载横幅或 MREC
下方代码展示了如何使用您的 Ad Unit ID、预期位置和 (横幅的) 预期背景颜色加载横幅或 MREC 广告。 MAX 会自动为您调整广告尺寸:
final String _banner_ad_unit_ID = Platform.isAndroid ? "«Android-banner-ad-unit-ID»" : "«iOS-banner-ad-unit-ID»";
function initializeBannerAds(){ // MAX automatically sizes banners to 320×50 on phones and 728×90 on tablets AppLovinMAX.createBanner(_banner_ad_unit_ID, AdViewPosition.bottomCenter);}
final String _mrec_ad_unit_ID = Platform.isAndroid ? "«Android-MREC-ad-unit-ID»" : "«iOS-MREC-ad-unit-ID»";
public void initializeMRecAds(){ // MAX sizes MRECs to 300×250 on phones and tablets AppLovinMAX.createMRec(_mrec_ad_unit_ID, AdViewPosition.bottomCenter);}
显示横幅
要显示横幅,请调用 showBanner()
:
AppLovinMAX.showBanner(_banner_ad_unit_ID);
要隐藏横幅,请调用 hideBanner()
:
AppLovinMAX.hideBanner(_banner_ad_unit_ID);
小组件方法
您也可以直接在小组件树中渲染横幅或 MREC 广告:
final String _ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";MaxAdView( adUnitId: _ad_unit_ID, adFormat: AdFormat.banner, listener: AdViewAdListener(onAdLoadedCallback: (ad) { ⋮ }, onAdLoadFailedCallback: (adUnitId, error) { ⋮ }, onAdClickedCallback: (ad) { ⋮ }, onAdExpandedCallback: (ad) { ⋮ }, onAdCollapsedCallback: (ad) { ⋮ }))
final String _ad_unit_ID = Platform.isAndroid ? "«Android-ad-unit-ID»" : "«iOS-ad-unit-ID»";MaxAdView( adUnitId: _ad_unit_ID, adFormat: AdFormat.mrec, listener: AdViewAdListener(onAdLoadedCallback: (ad) { ⋮ }, onAdLoadFailedCallback: (adUnitId, error) { ⋮ }, onAdClickedCallback: (ad) { ⋮ }, onAdExpandedCallback: (ad) { ⋮ }, onAdCollapsedCallback: (ad) { ⋮ }))
广告预加载
从 Flutter plugin 版本 3.11.1 开始,您可以在挂载 MaxAdView
之前预加载平台小组件的广告。
通过预加载 Ad Unit ID 挂载 MaxAdView
时,系统会使用预加载的平台小组件构架 MaxAdView
,
方便广告快速展示。
Flutter plugin 版本 4.1.0 更新了此预加载功能,支持多个 MaxAdView
实例。
preloadWidgetAdView()
方法会返回一个 AdViewId
,用来在挂载 MaxAdView
时指定预加载的广告。
AppLovinMAX.preloadWidgetAdView(«ad-unit-ID», AdFormat.banner) .then((adViewId) { print('Started preloading a banner ad $(adViewId) for ${«ad-unit-ID»}'); });
AppLovinMAX.preloadWidgetAdView(«ad-unit-ID», AdFormat.mrec) .then((adViewId) => { print('Started preloading a MREC ad $(adViewId) for ${«ad-unit-ID»}'); });
您可以在挂载 MaxAdView
时指定 AdViewId
:
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.banner, adViewId: «ad-view-ID», listener: AdViewAdListener(onAdLoadedCallback: (ad) { print('Banner widget ad (${ad.adViewId}) loaded from ${ad.networkName}'); }))
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.mrec, adViewId: «ad-view-ID», listener: AdViewAdListener(onAdLoadedCallback: (ad) { print('MREC widget ad (${ad.adViewId}) loaded from ${ad.networkName}'); }))
卸载 MaxAdView
时,预加载的平台小组件不会被销毁,而是会被缓存,供下一次挂载时再次使用。
要释放它的资源,请务必在不需要组件时手动将其销毁:
AppLovinMAX.destroyWidgetAdView(«ad-view-ID») .then((_) { print('Destroyed the preloaded banner ad'); });
AppLovinMAX.destroyWidgetAdView(«ad-view-ID») .then((_) { print('Destroyed the preloaded MREC ad'); });
如果在挂载 MaxAdView
时您省略了 AdViewId
,组件就会动态加载广告。
在这种情况下,如果您卸载 MaxAdView
,它就会被自动销毁,无法再次使用。
请参阅 AppLovin-MAX-Flutter GitHub 项目示例应用,了解完整的运行示例。
自适应横幅
停止和启动自动刷新
您可以停止广告的自动刷新。 当您隐藏广告或想要手动刷新时,可能会需要这个操作。 使用以下代码停止自动刷新:
AppLovinMAX.showBanner(«ad-unit-ID»);AppLovinMAX.stopBannerAutoRefresh(«ad-unit-ID»);
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.banner, isAutoRefreshEnabled: false, ⋮
AppLovinMAX.showMRec(«ad-unit-ID»);AppLovinMAX.stopMRecAutoRefresh(«ad-unit-ID»);
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.mrec, isAutoRefreshEnabled: false, ⋮
使用以下代码启动广告自动刷新:
AppLovinMAX.startBannerAutoRefresh(«ad-unit-ID»);
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.banner, isAutoRefreshEnabled: true, ⋮
AppLovinMAX.startMRecAutoRefresh(«ad-unit-ID»);
MaxAdView( adUnitId: «ad-unit-ID», adFormat: AdFormat.mrec, isAutoRefreshEnabled: true, ⋮