【问题标题】:How to show interstitial ads every 40 seconds in flutter dart如何在 flutter dart 中每 40 秒显示一次插页式广告
【发布时间】:2022-11-27 11:36:56
【问题描述】:

我有一个在 flutter 中使用 Admob 的插页式广告的工作代码,但是,只有当用户点击按钮时才能看到广告。我使用计时器每 40 秒显示一次广告,它在控制台中打印插页式广告已加载但广告并未在设备上展示。只希望广告每 40 秒显示一次, 谢谢

import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'dart:io' show Platform;
import 'colors.dart' as color;
import 'dart:async';

class adsinter extends StatefulWidget {
  const adsinter({Key? key}) : super(key: key);

  @override
  _adsinterState createState() => _adsinterState();
}

Timer? _timerForInter;

class _adsinterState extends State<adsinter> {
  //add Banner ad
  InterstitialAd? interstitialAd;
  bool isLoaded = false;

//   @override
//   void initState() {
//     // TODO: implement initState
// // Add these lines to launch timer on start of the app
//     _timerForInter = Timer.periodic(Duration(seconds: 20), (result) {
//       interstitialAd;
//     });
//     super.initState();
//   }

  @override
  void dispose() {
    // Add these to dispose to cancel timer when user leaves the app
    _timerForInter?.cancel();
    interstitialAd?.dispose();
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();

    _timerForInter = Timer.periodic(Duration(seconds: 10), (result) {
      String? getBannerAdUnitId() {
        String adiOS;
        String adAndroid;
        if (Platform.isAndroid) {
          // Android-specific code
          return "ca-app-pub-2014810929195140/8666570300";
        } else if (Platform.isIOS) {
          return "ca-app-pub-2014810929195140/7584778723";
          // iOS-specific code
        }
        return null;
      }

      InterstitialAd.load(
          adUnitId: getBannerAdUnitId().toString(),
          //"ca-app-pub-3940256099942544/1033173712",
          request: AdRequest(),
          adLoadCallback: InterstitialAdLoadCallback(
            onAdLoaded: (ad) {
              setState(() {
                isLoaded = true;
                this.interstitialAd = ad;
              });
              print("Interstitial Ad loaded");
            },
            onAdFailedToLoad: (error) {
              print('InterstitialAd failed to load: $error');
            },
          ));
    });
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Icon(
        Icons.loop,
        size: 30,
        color: color.AppColor.loopColor,
      ),
      onPressed: () {
        interstitialAd!.show();
      },
    );
  }
}

【问题讨论】:

    标签: flutter dart admob interstitial


    【解决方案1】:

    这段代码对我有用,可以在 android 上每 60 秒显示一次插页式广告

      Timer? _timerForInter; // <- Put this line on top of _MyAppState class
    
      @override
      void initState() {
        // Add these lines to launch timer on start of the app
        _timerForInter = Timer.periodic(Duration(seconds: 60), (result) {
          AdInterstitial1.loadInterstialAd();
          AdInterstitial1.showInterstitialAd();
        });
        super.initState();
      }
    
      @override
      void dispose() {
        // Add these to dispose to cancel timer when user leaves the app
        _timerForInter!.cancel();
        super.dispose();
      }
    

    【讨论】:

      【解决方案2】:

      在您的代码中,广告每 10 秒加载一次,但仅在您按下按钮时显示。您必须在 onAdLoaded 回调中调用 ad.show() 才能每 10 秒显示一次。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多