您可以在这里查看GitHub上插页式广告集成的代码示例。

InMobi SDK 无论如何都会提供回调,因此您可以选择不填写任何内容,完全在您自己的端处理奖励。
要创建激励视频广告,请创建以下实例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, 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">/**
* 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> {}
}
要请求插页式广告,请调用实例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, onAdLoaFailed, 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> {
interstitialAd = <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接口上的 `get_advertisement` 方法通知您的代码,并在onAdDisplayed广告实际显示在屏幕上时调用 `get_advertisement` 方法。如果广告无法显示,SDK 将生成一个事件,告知您的应用程序广告显示失败。您可以再次调用 `get_advertisement` 方法来处理此事件,onAdDisplayFailed从而请求新的广告。load()
当插页式广告被关闭时,您的应用程序将在回调中收到通知onAdDismissed。
对于激励视频广告,SDK 将生成onAdRewardActionCompleted事件。
<span class="hljs-title class_">InterstitialAdEventListener</span> mInterstitialAdEventListener = <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">// onAdLoadSucceeded, onAdDisplayed, etc </span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">onRewardsUnlocked</span>(<span class="hljs-params"><span class="hljs-meta">@NonNull</span> InMobiInterstitial inMobiInterstitial, <span class="hljs-built_in">Map</span> < <span class="hljs-built_in">Object</span>, <span class="hljs-built_in">Object</span> > map</span>) {
<span class="hljs-title class_">Log</span>.<span class="hljs-title function_">d</span>(<span class="hljs-variable constant_">TAG</span>, <span class="hljs-string">"Ad rewards unlocked!"</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-title class_">Object</span> <span class="hljs-attr">key</span>: map.<span class="hljs-title function_">keySet</span>()) {
<span class="hljs-title class_">Object</span> value = map.<span class="hljs-title function_">get</span>(key);
<span class="hljs-title class_">Log</span>.<span class="hljs-title function_">v</span>(<span class="hljs-variable constant_">TAG</span>, <span class="hljs-string">"Unlocked "</span> + value + <span class="hljs-string">" "</span> + key);
}
}
};
<span class="hljs-keyword">val</span> mInterstitialAdEventListener: InterstitialAdEventListener =
<span class="hljs-keyword">object</span> : InterstitialAdEventListener() {
<span class="hljs-comment">// implementation for other events </span>
<span class="hljs-comment">// onAdLoadSucceeded, onAdDisplayed, etc </span>
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onRewardsUnlocked</span><span class="hljs-params">( inMobiInterstitial: <span class="hljs-type">InMobiInterstitial</span>,
map: <span class="hljs-type">Map</span><<span class="hljs-type">any</span>, any=<span class="hljs-string">""</span>>
)</span></span> {
Log.d(TAG, <span class="hljs-string">"Ad rewards unlocked!"</span>)
<span class="hljs-keyword">for</span> (key <span class="hljs-keyword">in</span> map.keys) {
<span class="hljs-keyword">val</span> value = map[key]
Log.v(TAG, <span class="hljs-string">"Unlocked <span class="hljs-variable">$value</span> <span class="hljs-variable">$key</span>"</span>)
}
}
}
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