要创建插页式广告,请创建一个实例InMobiInterstitial:
<span class="hljs-type">InMobiInterstitial</span> <span class="hljs-variable">interstitialAd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiInterstitial</span>(InterstitialAdsActivity.<span class="hljs-built_in">this</span>, <span class="hljs-number">1471550843414L</span>,
mInterstitialAdEventListener);
<span class="hljs-keyword">val</span> interstitialAd = InMobiInterstitial(<span class="hljs-keyword">this</span><span class="hljs-symbol">@InterstitialAdsActivity</span>, <span class="hljs-number">1471550843414L</span>, mInterstitialAdEventListener )
这里mInterstitialAdEventListener是抽象类的一个实现InterstitialAdEventListener。创建插页式广告必须提供此抽象类的实现。
InterstitialAdEventListener抽象类允许您监听插页式广告的关键生命周期事件。您应该监听这些事件,以确保您的应用程序能够正确处理各种生命周期转换。
<span class="hljs-comment">/**
* Listener for receiving notifications during the lifecycle of an interstitial.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">InterstitialAdEventListener</span> {
<span class="hljs-comment">/**
* Called to indicate that an ad was loaded and it can now be shown. This will always be called
* after the {<span class="hljs-doctag">@link</span> ##onAdFetchSuccessful(InMobiInterstitial, AdMetaInfo)} callback.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which was loaded
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadSucceeded</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
<span class="hljs-comment">/**
* Callback to signal that a request to fetch an ad (by calling
* {<span class="hljs-doctag">@link</span> InMobiInterstitial##load()} failed. The status code indicating the reason for failure
* is available as a parameter. You should call {<span class="hljs-doctag">@link</span> InMobiInterstitial##load()} again to
* request a fresh ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which failed to load
* <span class="hljs-doctag">@param</span> status Represents the {<span class="hljs-doctag">@link</span> InMobiAdRequestStatus} status containing error reason
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadFailed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, <span class="hljs-meta">@NonNull</span> InMobiAdRequestStatus status)</span> {}
<span class="hljs-comment">/**
* Called to indicate that an ad is available in response to a request for an ad (by calling
* {<span class="hljs-doctag">@link</span> InMobiInterstitial##load()}. Note: This does not
* indicate that the ad can be shown yet. Your code should show an ad after the
* {<span class="hljs-doctag">@link</span> ##onAdLoadSucceeded(InMobiInterstitial)} method is called. Alternately, if you do not
* want to handle this event, you must test if the ad is ready to be shown by checking the
* result of calling the {<span class="hljs-doctag">@link</span> InMobiInterstitial##isReady()} method.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad for which ad content was received
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdFetchSuccessful</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
<span class="hljs-comment">/**
* Called to indicate that an ad interaction was observed.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad on which user clicked
* <span class="hljs-doctag">@param</span> params Represents the click parameters
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdClicked</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, Map < Object, Object > params)</span> {}
<span class="hljs-comment">/**
* Called to indicate that the ad will be launching a fullscreen overlay.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which will display
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdWillDisplay</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad)</span> {}
<span class="hljs-comment">/**
* Called to indicate that the fullscreen overlay is now the topmost screen.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which is displayed
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdDisplayed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
<span class="hljs-comment">/**
* Called to indicate that a request to show an ad (by calling {<span class="hljs-doctag">@link</span> InMobiInterstitial##show()}
* failed. You should call {<span class="hljs-doctag">@link</span> InMobiInterstitial##load()} to request for a fresh ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which failed to show
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdDisplayFailed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad)</span> {}
<span class="hljs-comment">/**
* Called to indicate that the fullscreen overlay opened by the ad was closed.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad which was dismissed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdDismissed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad)</span> {}
<span class="hljs-comment">/**
* Called to indicate that the user may leave the application on account of interacting with the ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onUserLeftApplication</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad)</span> {}
<span class="hljs-comment">/**
* Called to indicate that rewards have been unlocked.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiInterstitial} ad for which rewards was unlocked
* <span class="hljs-doctag">@param</span> rewards Represents the rewards unlocked
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onRewardsUnlocked</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad, Map < Object, Object > rewards)</span> {}
<span class="hljs-comment">/**
* Called to notify that inmobi has logged an impression for the ad
* <span class="hljs-doctag">@param</span> ad Represents the ad which was impressed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdImpression</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial ad)</span> {
}
}
<span class="hljs-comment">/**
* Listener for receiving notifications during the lifecycle of an interstitial.
*/</span>
<span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">InterstitialAdEventListener</span> : <span class="hljs-type">AdEventListener</span><<span class="hljs-type">inmobiinterstitial</span>>() {
<span class="hljs-comment">/**
* Called to notify that an ad preload has failed.
*
***Note** This notification is given only when you use `preload()` in
* [InMobiInterstitial.preloadManager]
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad which was preloaded
* <span class="hljs-doctag">@param</span> status Represents the [InMobiAdRequestStatus] status containing error reason
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFetchFailed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>, status: <span class="hljs-type">InMobiAdRequestStatus</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that the ad will be launching a fullscreen overlay.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad which will display
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdWillDisplay</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that the fullscreen overlay is now the topmost screen.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad which is displayed
* <span class="hljs-doctag">@param</span> info Represents the meta info for the ad displayed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdDisplayed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>, info: <span class="hljs-type">AdMetaInfo</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that a request to show an ad (by calling [InMobiInterstitial.show]
* failed. You should call [InMobiInterstitial.load] to request for a fresh ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad which failed to show
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdDisplayFailed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that the fullscreen overlay opened by the ad was closed.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad which was dismissed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdDismissed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that the user may leave the application on account of interacting with the ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onUserLeftApplication</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to indicate that rewards have been unlocked.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiInterstitial] ad for which rewards was unlocked
* <span class="hljs-doctag">@param</span> rewards Represents the rewards unlocked
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onRewardsUnlocked</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>, rewards: <span class="hljs-type">Map</span><<span class="hljs-type">any</span>, any=<span class="hljs-string">""</span>>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that inmobi has logged an impression for the ad
* <span class="hljs-doctag">@param</span> ad Represents the ad which was impressed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdImpression</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiInterstitial</span>)</span></span> {}
}
要请求插页式广告,请调用实例load()上的相应方法InMobiInterstitial:
interstitialAd.load();
interstitialAd.<span class="hljs-built_in">load</span>()
InterstitialAdEventListener抽象类会通知此方法的结果。如果广告请求成功,onAdFetchSuccessful(InMobiInterstitial, AdMetaInfo)则会生成一个事件。随后会生成另一个onAdLoadSucceeded事件onAdLoadFailed。
如果事件被触发,则事件触发时传递的对象onAdLoadFailed会指示触发原因。InMobiAdRequestStatus
如果事件已生成,您可以在此后的任何时间通过调用实例onAdLoadSucceeded来显示广告。show()InMobiInterstitial
在 SDK 生成事件后再次调用load()同一个插页式广告onAdLoadSucceeded并不能保证会从广告网络请求到新的广告。只有当之前的广告被 SDK 丢弃、广告过期或用户清除了应用数据和缓存时,才会获取到新的广告。
要显示插页式广告,可以在 SDK 生成事件后的任何时间show()调用实例。以下代码片段对此进行了说明。InMobiInterstitialonAdLoadSucceeded
InMobiInterstitial interstitialAd;
<span class="hljs-type">InterstitialAdEventListener</span> <span class="hljs-variable">mInterstitialAdEventListener</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InterstitialAdEventListener</span>() {
<span class="hljs-comment">// implementation for other events </span>
<span class="hljs-comment">// onAdFetchSuccessful, onAdLoadFailed, etc </span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadSucceeded</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiInterstitial inMobiInterstitial, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {
Log.d(TAG, <span class="hljs-string">"Ad can now be shown!"</span>);
mCanShowAd = <span class="hljs-literal">true</span>;
}
};
<span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">()</span> {
<span class="hljs-type">InMobiInterstitial</span> <span class="hljs-variable">interstitialAd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiInterstitial</span>(GameActivity.<span class="hljs-built_in">this</span>, <span class="hljs-number">1471550843414</span> L, mInterstitialAdEventListener);
}
<span class="hljs-keyword">void</span> <span class="hljs-title function_">prepareGameLevel</span><span class="hljs-params">()</span> {
interstitialAd.load();
}
<span class="hljs-keyword">void</span> <span class="hljs-title function_">handleGameLevelCompleted</span><span class="hljs-params">()</span> {
If(mCanShowAd) interstitialAd.show();
}
<span class="hljs-keyword">val</span> interstitialAd: InMobiInterstitial
<span class="hljs-keyword">val</span> mInterstitialAdEventListener = InterstitialAdEventListener() {
<span class="hljs-comment">// implementation for other events </span>
<span class="hljs-comment">// onAdFetchSuccessful, onAdLoadFailed, etc </span>
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdLoadSucceeded</span><span class="hljs-params">(inMobiInterstitial: <span class="hljs-type">InMobiInterstitial</span>, info: <span class="hljs-type">AdMetaInfo</span>)</span></span> {
Log.d(TAG, <span class="hljs-string">"Ad can now be shown!"</span>)
mCanShowAd = <span class="hljs-literal">true</span>
}
}
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">init</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">val</span> interstitialAd = InMobiInterstitial(<span class="hljs-keyword">this</span><span class="hljs-symbol">@GameActivity</span>, <span class="hljs-number">1471550843414L</span>, mInterstitialAdEventListener)
}
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">prepareGameLevel</span><span class="hljs-params">()</span></span> {
interstitialAd.load()
}
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">handleGameLevelCompleted</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">if</span>(mCanShowAd) interstitialAd.show()
}
您的应用程序将通过抽象类收到此请求的结果通知InterstitialAdEventListener。
如果广告可以显示,SDK 将通过调用接口通知您的代码onAdWillDisplay,然后在onAdDisplayed广告实际显示在屏幕上时调用该方法。
如果广告无法显示,onAdDisplayFailedSDK 会生成一个事件来通知您的应用程序广告无法显示。您可以load()再次调用该事件处理程序来请求显示新的广告。
当插页式广告被关闭时,您的应用程序将在回调中收到通知onAdDismissed。
By installing this SDK update, you agree that your Children Privacy Compliance setting remains accurate or that you will update that setting, whenever there is a change in your app's audience. You may update the app's Children Privacy Compliance settings at https://publisher.inmobi.com/my-inventory/app-and-placements.
Support Center