Android应用程序进入后台时如何暂停插页式广告?

How to pause interstitial Ads when Android application goes to background?(Android应用程序进入后台时如何暂停插页式广告?)
本文介绍了Android应用程序进入后台时如何暂停插页式广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用适用于 Android 的 admob 的插页式广告方面遇到问题,即使我点击主页按钮或转到后台,商业视频仍在播放.有没有人知道如何解决这个问题?

I have an issues with interstitial ad using admob for Android, which is the commercial video still running even I tap home button or goes to background. Is there anyone knows how to fix this issue?

我已经搜索了一整天,但找不到它.有人说我们无法控制广告.但是当我们点击设备上的主页按钮或应用程序进入后台时,Youtube android 应用程序能够暂停.

I've searched all day long, but couldn't find it. Some says that we can't control the ad. But when Youtube android application is able to pause whenever we tap on Home Button on device or the app goes to background.

推荐答案

您使用的是什么版本的 AdMob SDK?此解决方案适用于 Google Play 服务.

What version of the AdMob SDK are you using? This solution will work with Google Play Services.

假设这是您设置插页式广告的方式:

Lets say that this is how you setup your interstitial:

//Here is where you setup your interstitial
//...
adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded(){
        adView.show();
    }
});

在您的 onPause() 方法中,您可以执行以下操作:

In your onPause() method you can do the following:

protected void onPause() {
    super.onPause();

    if(adView != null) {
        adView.setAdListener(null);
    }
}

之所以有效,是因为当 onPause 方法因新的插页式广告而被调用时,广告已经处于展示过程中并且会正常展示.但是,如果由于其他原因(例如按下 Home 按钮)调用 onPause(),它将禁止显示新的插页式广告.

This works because by the time the onPause method is called because of a new interstitial, the ad will already be in the process of being shown and will display normally. However, if onPause() is called because of something else, like the Home button being pressed, it will disable new interstitials from being shown.

唯一需要注意的是,如果您使用的是 AdListener 提供的其他回调方法.

Only caveat is if you're using other callback methods available from the AdListener.

这篇关于Android应用程序进入后台时如何暂停插页式广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)