android 优化Flutter上的Google移动的广告初始化和加载

xzv2uavs  于 2023-05-05  发布在  Android
关注(0)|答案(2)|浏览(211)

bounty还有4天到期。回答此问题可获得+50声望奖励。under正在寻找一个答案从一个有信誉的来源

我在一个针对Android的Flutter应用程序中使用google_mobile_ads Flutter包(版本^3.0.0)。我偶尔会收到可能与广告有关的ANR。
This文章讨论了优化Google移动的广告初始化和Android上的加载,以防止ANR。
考虑到我使用的是Flutter而不是本文所针对的原生Android应用程序,我是否应该将这些选项添加到我的Android清单中?

<manifest>
  ...
  <application>
      ...
      <meta-data
          android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
          android:value="true"/>
     <meta-data
          android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
          android:value="true"/>

  </application>
</manifest>
wdebmtf2

wdebmtf21#

一种方法是使用变量isBannerLoaded沿着BannerAdListener

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';

class AdBanner extends StatefulWidget {
  final String adUnitId;
  final AdSize size;
  const AdBanner({this.adUnitId, this.size});

  @override
  State<AdBanner> createState() => _AdBannerState();

  static AdSize getBannerSize() {
    final width =
        MediaQueryData.fromWindow(WidgetsBinding.instance.window).size.width;
    final height =
        MediaQueryData.fromWindow(WidgetsBinding.instance.window).size.height;
    if ((width >= 460 && width <= 500) || (height >= 800 && height <= 900)) {
      return AdSize(width: 336, height: 280);
    } else if ((width >= 360 && width <= 375) ||
        (height >= 600 && height <= 700)) {
      return AdSize(width: 320, height: 100);
    } else if ((width >= 375 && width <= 414) ||
        (height >= 700 && height <= 800)) {
      return AdSize(width: 300, height: 250);
    } else if ((width >= 300 && width <= 360) ||
        (height >= 400 && height <= 600)) {
      return AdSize(width: 320, height: 50);
    } else {
      return AdSize(width: 320, height: 50);
    }
  }
}

class _AdBannerState extends State<AdBanner> {
  bool isBannerLoaded = false;
  BannerAd myBanner;
  @override
  Widget build(BuildContext context) {
    print('AdBanner build : ' + widget.adUnitId);
   
    final BannerAd bannerAd = myBanner;
    if (isBannerLoaded && bannerAd != null) {
      return Container(
          height: bannerAd.size.height.toDouble(),
          width: bannerAd.size.width.toDouble(),
          child: AdWidget(ad: bannerAd));
    } else {
      return Container(
      );
    }

   
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    myBanner = BannerAd(
      adUnitId: widget.adUnitId ?? '/234721286/NY_KB_Home_Micrec_23',
      size: widget.size ?? AdSize.banner,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) {
          print('AdBanner onAdLoaded : ' + ad.adUnitId);
          setState(() {
            isBannerLoaded = true;
          });
        },
        // Called when an ad request failed.
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('inside failed');
          // Dispose the ad here to free resources.
          ad.dispose();
          print('failed to load: $error');
        },
        // Called when an ad opens an overlay that covers the screen.
        onAdOpened: (Ad ad) => print('Ad opened.'),
        // Called when an ad removes an overlay that covers the screen.
        onAdClosed: (Ad ad) => print('Ad closed.'),
        // Called when an impression occurs on the ad.
        onAdImpression: (Ad ad) => print('Ad impression.'),
      ),
    )..load();
  }

  @override
  void dispose() {
    super.dispose();
    myBanner.dispose();
  }
}
abithluo

abithluo2#

考虑到google_mobile_ads Flutter包确实使用了Google移动的Ads SDK for Android,这些优化应该适用。
使用这个有什么不好的地方吗?如果它只是使加载更好而没有任何潜在的“副作用”,为什么默认情况下不启用?
described here一样,激活这些优化意味着需要延长处理时间的初始化和广告加载任务将被卸载到后台线程。
This thread没有提到任何缺点,参考这个"Get started" guide
这意味着它不是默认的,因为这些优化仍处于Beta阶段。
昨天发布的new testing features for ad inspector可以帮助您验证这些优化效果。

相关问题