firebase - 如何在flutter dart中每20秒展示一次插页式广告

标签 firebase flutter dart admob interstitial

我在使用 flutter 实现插页式定时广告时遇到问题。我已经实现了一个选项,该选项在单击按钮时显示广告,但我想实现每 20 秒显示广告一次。请帮忙

我使用的是我在网上找到的下面的代码

import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';

const String testDevice = 'MobileId';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
    testDevices: testDevice != null ? <String>[testDevice] : null,
    nonPersonalizedAds: true,
    keywords: <String>['Game', 'Mario'],
  );

  BannerAd _bannerAd;
  InterstitialAd _interstitialAd;

  BannerAd createBannerAd() {
    return BannerAd(
        adUnitId: BannerAd.testAdUnitId,
      //Change BannerAd adUnitId with Admob ID
        size: AdSize.banner,
        targetingInfo: targetingInfo,
        listener: (MobileAdEvent event) {
          print("BannerAd $event");
        });
  }

  InterstitialAd createInterstitialAd() {
    return InterstitialAd(
        adUnitId: InterstitialAd.testAdUnitId,
      //Change Interstitial AdUnitId with Admob ID
        targetingInfo: targetingInfo,
        listener: (MobileAdEvent event) {
          print("IntersttialAd $event");
        });
  }

  @override
  void initState() {
    FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId);
    //Change appId With Admob Id
    _bannerAd = createBannerAd()
      ..load()
      ..show();
    super.initState();
  }

  @override
  void dispose() {
    _bannerAd.dispose();
    _interstitialAd.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(),
      home: Scaffold(
        appBar: AppBar(
          title: Text("Demo App"),
        ),
        body: Center(
            child: RaisedButton(
          child: Text('Click on Ads'),
          onPressed: () {
            createInterstitialAd()
              ..load()
              ..show();
          },
        )),
      ),
    );
  }
}

我使用的是flutter语言,网上的教程还不是很多,因为社区仍在发展

最佳答案

尝试将以下内容添加到您的代码中:

import 'dart:async'; // <-- put  it on very top of your file

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: 20), (result) {
  _interstitialAd = createInterstitialAd()..load();
  });
  super.initState();
 }

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

关于firebase - 如何在flutter dart中每20秒展示一次插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61460602/

相关文章:

swift - 数据仅一次附加到我的tableView和Firebase数据库中

android - 删除并重新插入 GeoFire 位置以触发 onKeyExited 和 onKeyEntered 不起作用

dart - 如何将图像背景分配给 flutter 中的列

class - 定义类的要点

flutter - 如何在 Flutter 项目中从 Firebase 获取自动生成的 key ?

google-app-engine - 将 SSL 与 Dart 和托管虚拟机一起使用

Swift - Firebase 快照到变量中

flutter_ffmpeg 包名

gradle - JetifyTransform Flutter 运行执行失败

swift - iOS : Firebase phone Authenication