如何在飞镖上显示插页式广告每20秒钟

问题描述 投票:1回答:1

我在实施插页式定时广告时遇到了问题。我已经实现了一个选项,该选项可以在单击按钮时显示广告,但是我想实现每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();
          },
        )),
      ),
    );
  }
}

我使用的是浮动语言,由于社区仍在增长,在线教程还不多

firebase flutter dart admob interstitial
1个回答
0
投票

尝试在代码中添加以下内容:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.