插屏广告是一种全屏展现的广告类型,同时支持静态(图片)和动态(视频)两种内容形式。 插屏广告格式举例:
SDK会触发一系列事件来通知应用程序插屏广告的活动,如广告库存状态、广告播放完成等事件,开发者可以通过事件来获知广告是否准备好,以及广告是否被点击。所以,设置和实现插屏广告回调Listener的接口方法,是使用插屏广告的必要操作。下面的代码片段演示了如何实现InterstitialAdListener接口来接收和处理插屏广告事件。
插屏触发的所有事件都可以在下面代码中找到。
InterstitialAd.setAdListener(new InterstitialAdListener() {
/**
* Invoked when the interstitial ad availability status is changed.
*
* @param - available is a boolean.
* True: means the interstitial ad is available and you can
* show the video by calling InterstitialAd.showAd().
* False: means no ad are available
*/
@Override
public void onInterstitialAdAvailabilityChanged(boolean available) {
// Change the interstitial ad state in app according to param available.
}
/**
* Invoked when the Interstitial ad view has opened.
* Your activity will lose focus.
*/
@Override
public void onInterstitialAdShowed(Scene scene) {
// Do not perform heavy tasks till the ad is going to be closed.
}
/**
* Invoked when the Interstitial ad is to be closed.
* Your activity will regain focus.
*/
@Override
public void onInterstitialAdClosed(Scene scene) {
}
/**
* Invoked when the user clicked on the Interstitial ad.
*/
@Override
public void onInterstitialAdClicked(Scene scene) {
}
/* Invoked when the call to load or show an Interstitial ad has failed
* @param - error contains the reason for the failure:
*/
@Override
public void onInterstitialAdShowFailed(Error error) {
// Interstitial ad shows failed
}
})
WARNING
注意: InterstitialAdListener为全局回调,只需要设置一次,若有多处展示场景,可根据回调用的scene进行区分。
若多次设置setAdListener,则每个InterstitialAdListener均会回调!
onInterstitialAdShowFailed回调中的Error参数包含失败的原因,参考错误码。
广告可用检查
Mint Mediation SDK智能库存引擎负责广告库存的维护,您只需完成SDK集成和初始化,SDK会自动加载广告。通过实现InterstitialAdListener接口,应用程序将会收到广告可用性变化的事件通知,通过 onInterstitialAdAvailabilityChanged接口的available参数获取到当前广告是否可用.
public void onInterstitialAdAvailabilityChanged(boolean available)
当然,您也可以通过直接调用isReady(String scene) 方法来检查广告库存状态,如下所示。
public boolean InterstitialAd.isReady(String scene)
展示广告
一旦收到onInterstitialoAvailabilityChanged 事件的true回调,您就可以调用showAd() 方法进行广告的展示。当然我们并不建议这么做,Availability事件只有在广告可用性发生变化的时候才会产生,这并不一定符合应用设计的广告场景,而且有可能造成频繁的甚至连续的广告展示,这会给用户产生困扰,影响应用的使用体验。您应该根据应用中的广告场景设计来选择广告展示时机。
//if you would like to show ad right after it's was loaded
public void onInterstitialAdAvailabilityChanged(boolean available) {
if(available) {
InterstitialAd.showAd(scene);
}
}
WARNING
在 onInterstitialAdAvailabilityChanged 回调中展示广告可能会导致不可预见的行为。一般情况您不应这么做,除非在某些特定的场景并且对showAd方法的调用进行了必要的限制。
如果您是想在需要的情况下(应用设计的广告场景)进行广告展示,我们强烈建议您在进行广告展示之前,先通过调用isReady() 方法检查广告是否可用,如下所示:
//if you would like to show ad when it's required
if (InterstitialAd.isReady(scene)) {
InterstitialAd.showAd(scene);
}
WARNING
当您成功的完成步骤2,意味着广告已经展示成功。如果您想展示另外的广告,只需重复步骤2进行展示即可,不需要手动调用loadAd方法进行广告加载。
如果您聚合了Admob广告,您需要确保对isReady 和 showAd 方法的调用必须在主线程中进行。
完成!
现在您已经可以在应用中展示插屏广告了。
您可以根据我们的集成文档来聚合其他广告类型,或者测试您的集成: