本文主要是介绍flutter如何实现一个应用位于前台时全局页面每隔三分钟弹出一次一天最多弹出5次的GroMore半插屏广告,处于付费页和后台时停止,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,首先添加一个全局的生命周期监听类
class AppLifecycleObserver with WidgetsBindingObserver {bool IsCold = false;bool isAgree = false;void getIsCold() async {isAgree = await SPManager().getBool(SPKeys.isAgreePrivacy, defaultValue: true);IsCold = await SPManager().getBool("isCold", defaultValue: false);}Future<bool> getIsVipPay() async {return await SPManager().getBool('isVipPay', defaultValue: false);}@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {super.didChangeAppLifecycleState(state);getIsCold();if(!isAgree) {if (state == AppLifecycleState.resumed) {if (!IsCold) {//热启动开屏广告IdcardAndroid().showSplashAd(AdsConfig.splashId);SPManager().setBool("isCold", true);} else {SPManager().setBool("isCold", AdsConfig.isPayBack? true: false);}SPManager().getBool(SPKeys.isShowPayPage, defaultValue: false).then((value) => {if (value){//满足条件开始计时GlobalAdsTimer.showScreenAds()}});}}if (state == AppLifecycleState.paused){//退入后台时停止广告计时GlobalAdsTimer.stop();} else if (state == AppLifecycleState.detached) {}}}
在main()入口绑定 就可以监测到了 全局的生命周期
`WidgetsBinding.instance.addObserver(AppLifecycleObserver());
2,定义一个全局的计时器 GlobalAdsTimer()
class GlobalAdsTimer {static Timer? timerHalfScreen;//展示广告static void start() {timerHalfScreen?.cancel();timerHalfScreen = null;timerHalfScreen = Timer.periodic(const Duration(minutes: 3), (timer) {showTodayHalfScreenVideo();setRealSystemUIOverlayStyle = true;addTodayHalfScreenNumVideo();timerHalfScreen?.cancel();timerHalfScreen = null;GroMoreManager().showInterstitialAd((b){//如果当前有前一个插屏广告未关闭就不继续请求if (b){showScreenAds();}});});}//展示条件static void showScreenAds() {print('GlobalAdsTimer_showScreenAds');GroMoreManager().isShowHomeInterstitialAd().then((value) => {if (value) {getShowTodayHalfScreenVideo().then((value) => {if (value) {clearTodayHalfScreenNumVideo(0),},getShowTodayHalfScreenNumVideo().then((int num) => {if (num < 5) {start(),} else {timerHalfScreen?.cancel(),timerHalfScreen = null,}})})}});}// 停止广告 应用退入后台/进入通用付费页/引导付费页 停止计时static void stop() {print('GlobalAdsTimer_stop');timerHalfScreen?.cancel();}
3,当进入特殊页面禁止弹出插屏广告,例如付费页禁止弹出影响用户付费率时
VipPage
@overridevoid initState() {super.initState();//停止计时GlobalAdsTimer.stop(); }
页面销毁时开始
@overridevoid dispose() {super.dispose();GlobalAdsTimer.showScreenAds();}
这篇关于flutter如何实现一个应用位于前台时全局页面每隔三分钟弹出一次一天最多弹出5次的GroMore半插屏广告,处于付费页和后台时停止的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!