InMobi原生广告支持静态广告和视频广告。您可以选择信息流广告、启动画面广告或前置广告格式。创建原生广告位的步骤如下。
InMobi原生广告支持静态广告和视频广告。您可以选择信息流广告、启动广告或前置广告格式。创建原生广告位的步骤如下。
原生广告位创建完成后,您将能够看到广告位 ID。

要创建原生广告,请创建一个InMobiNative实例:
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">NativeAdsActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Activity</span> {
……………………
<span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> List<InMobiNative> mNativeAds = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span><>();
……………………
<span class="hljs-type">InMobiNative</span> <span class="hljs-variable">nativeAd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiNative</span>(NativeAdsActivity.<span class="hljs-built_in">this</span>, YOUR_PLACEMENT_ID, nativeAdEventListener);
nativeAd.load();
mNativeAds.add(nativeAd);
……………………
}
<span class="hljs-keyword">class</span> <span class="hljs-title class_">NativeAdsActivity</span> : <span class="hljs-type">Activity</span> {
……………………
<span class="hljs-keyword">private</span> <span class="hljs-keyword">val</span> mNativeAds : List<inmobinative> = ArrayList()
……………………
<span class="hljs-keyword">val</span> nativeAd = InMobiNative(<span class="hljs-keyword">this</span><span class="hljs-symbol">@NativeAdsActivity</span>, YOUR_PLACEMENT_ID, nativeAdEventListener)
nativeAd.load()
mNativeAds.add(nativeAd)
……………………
}
以下nativeAdEventListener是抽象类的一个实现NativeAdEventListener。创建原生广告必须提供此接口的实现。如果在未进行 SDK 初始化的情况下创建 InMobiNative 对象,将会抛出异常。
您必须向InMobiNative构造函数提供一个 Context 实现。请确保您的实现持有强引用InMobiNative,否则您将无法从 SDK 获取此引用的任何回调。这可以通过将其设为类的成员变量或将其存储在贯穿应用程序生命周期的集合中来实现。
inMobiNative应该始终是一个强引用,否则您可能不会收到此引用的任何回调(加载成功、加载失败等)。inMobiNative被任何变量引用,请确保它是强引用。inMobiNative不会因为垃圾回收而被删除。NativeAdEventListener抽象类允许您监听原生广告的关键生命周期事件。
<span class="hljs-comment">/**
* A listener for receiving notifications during the lifecycle of a Native ad.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">NativeAdEventListener</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> ##onAdReceived(InMobiNative)} callback.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad which was loaded
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadSucceeded</span><span class="hljs-params">(InMobiNative ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that a native ad failed to load.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad which failed to load
* <span class="hljs-doctag">@param</span> requestStatus 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">(InMobiNative ad, InMobiAdRequestStatus requestStatus)</span> {}
<span class="hljs-comment">/**
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad whose fullscreen was dismissed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdFullScreenDismissed</span><span class="hljs-params">(InMobiNative ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that the ad opened an overlay that covers the screen.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad whose fullscreen will be displayed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdFullScreenDisplayed</span><span class="hljs-params">(InMobiNative ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that the user is about to leave the application as a result of interacting with it.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onUserWillLeaveApplication</span><span class="hljs-params">(InMobiNative ad)</span> {}
<span class="hljs-comment">/**
* Called to notify ad was clicked. Note:Override this method to notify click to the Mediation Adapter.
*
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiNative} ad which was clicked
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdClicked</span><span class="hljs-params">(InMobiNative ad)</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> InMobiNative ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that an ad was received successfully but is not ready to be displayed yet.
*
* <span class="hljs-doctag">@param</span> ad Represents the ad which was loaded or preloaded
* <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> T ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
}
<span class="hljs-comment">/**
* A listener for receiving notifications during the lifecycle of a Native ad.
*/</span>
<span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">NativeAdEventListener</span> : <span class="hljs-type">AdEventListener</span><<span class="hljs-type">InMobiNative</span>>() {
<span class="hljs-comment">/**
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiNative] ad whose fullscreen was dismissed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFullScreenDismissed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiNative</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that the ad opened an overlay that covers the screen.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiNative] ad whose fullscreen will be displayed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFullScreenDisplayed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiNative</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that the user is about to leave the application as a result of interacting with it.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiNative] ad
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onUserWillLeaveApplication</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiNative</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify ad was clicked. **Note:**Override this method to notify click to the Mediation Adapter.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiNative] ad which was clicked
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdClicked</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiNative</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">InMobiNative</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that an ad was received successfully but is not ready to be displayed yet.
*
* <span class="hljs-doctag">@param</span> ad Represents the ad which was loaded or preloaded
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFetchSuccessful</span><span class="hljs-params">(ad: <span class="hljs-type">T</span>,info: <span class="hljs-type">AdMetaInfog</span>)</span></span> {}
}
VideoEventListener抽象类允许您监听原生广告的视频生命周期事件。
<span class="hljs-comment">/**
* Listener interface for receiving video lifecycle and interaction events.
*
* This interface provides callbacks for various video playback states and user interactions
* with video advertisements. Implementers can use these events to track video performance,
* handle audio state changes, and respond to video playback lifecycle events for analytics
* and user experience optimization.
*/</span>
<span class="hljs-keyword">interface</span> <span class="hljs-title class_">VideoEventListener</span> {
<span class="hljs-comment">/**
* Called when video playback has started.
*
* This method is invoked when the video begins playing for the first time.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onVideoStarted</span><span class="hljs-params">()</span>
<span class="hljs-comment">/**
* Called when video playback has resumed from a paused state.
*
* This method is invoked when the video continues playing after being paused.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onVideoResumed</span><span class="hljs-params">()</span>
<span class="hljs-comment">/**
* Called when video playback has been paused.
*
* This method is invoked when the video is temporarily stopped by user action
* or programmatic control.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onVideoPaused</span><span class="hljs-params">()</span>
<span class="hljs-comment">/**
* Called when video playback has completed successfully.
*
* This method is invoked when the video has played to its end.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onVideoCompleted</span><span class="hljs-params">()</span>
<span class="hljs-comment">/**
* Called when the audio state of the video has changed.
*
* This method is invoked whenever the video's audio is muted or unmuted,
* allowing implementers to track audio interaction patterns.
*
* <span class="hljs-doctag">@param</span> isMuted true if the video is currently muted, false if audio is enabled
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAudioStateChanged</span><span class="hljs-params">(<span class="hljs-type">boolean</span> isMuted)</span>
}
<span class="hljs-comment">/**
* Listener interface for receiving video lifecycle and interaction events.
*
* This interface provides callbacks for various video playback states and user interactions
* with video advertisements. Implementers can use these events to track video performance,
* handle audio state changes, and respond to video playback lifecycle events for analytics
* and user experience optimization.
*/</span>
<span class="hljs-keyword">interface</span> <span class="hljs-title class_">VideoEventListener</span> {
<span class="hljs-comment">/**
* Called when video playback has started.
*
* This method is invoked when the video begins playing for the first time.
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onVideoStarted</span><span class="hljs-params">()</span></span>
<span class="hljs-comment">/**
* Called when video playback has resumed from a paused state.
*
* This method is invoked when the video continues playing after being paused.
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onVideoResumed</span><span class="hljs-params">()</span></span>
<span class="hljs-comment">/**
* Called when video playback has been paused.
*
* This method is invoked when the video is temporarily stopped by user action
* or programmatic control.
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onVideoPaused</span><span class="hljs-params">()</span></span>
<span class="hljs-comment">/**
* Called when video playback has completed successfully.
*
* This method is invoked when the video has played to its end.
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onVideoCompleted</span><span class="hljs-params">()</span></span>
<span class="hljs-comment">/**
* Called when the audio state of the video has changed.
*
* This method is invoked whenever the video's audio is muted or unmuted,
* allowing implementers to track audio interaction patterns.
*
* <span class="hljs-doctag">@param</span> isMuted true if the video is currently muted, false if audio is enabled
*/</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAudioStateChanged</span><span class="hljs-params">(isMuted: <span class="hljs-type">Boolean</span>)</span></span>
}
InMobiNative要请求原生广告,请对您上面创建的实例调用 load() 方法:
nativeAd.load();
nativeAd.<span class="hljs-built_in">load</span>()
NativeAdEventListener 抽象类会通知此方法的结果。如果广告请求成功,onAdReceived(InMobiNative)则会生成一个事件。随后会生成另一个onAdLoadSucceeded事件onAdLoadFailed。如果生成了另一个事件,则该事件传递的对象onAdLoadFailed会指示请求成功的原因。InMobiAdRequestStatus
要接收视频事件,请使用上面创建的实例上的(VideoEventListener )videoEventListener方法进行注册:setVideoEventListenervideoEventListenerInMobiNative
nativeAd.setVideoEventListener (videoEventListener);
nativeAd<span class="hljs-selector-class">.setVideoEventListener</span> (videoEventListener)
抽象VideoEventListener类会通知您有关原生广告的视频事件。
以下是一些InMobiNative有助于拼接广告的重要 API:
String getAdTitle()- 返回广告的标题文本。String getAdDescription()- 返回广告的描述文本。AdAsset getAdIcon()- 返回图标资源对象。用于getAdIcon().url 获取图标图像 URL。String getCtaText()- 返回广告的号召性用语文本。Float getAdRating()- 返回广告评分(可选,如果无评分则返回 0)。String getAdvertiserName()- 返回广告的广告商/赞助商名称。View getMediaView()- 返回包含广告图片或视频内容的媒体视图。此视图应添加到您的媒体容器(FrameLayout)中。View getAdChoiceIcon()- 返回 AdChoices 图标视图。请将其添加到您的 AdChoices 容器中。boolean isReady()- 当广告准备好显示时,返回 true。Boolean isVideo()- 如果广告包含视频内容,则返回 true;否则返回 false;如果在无效状态下调用,则返回 null。void registerViewForTracking(InMobiNativeViewData viewData)- 将您的自定义广告视图注册到 InMobi SDK,以便进行展示跟踪和点击处理。这是至关重要的一步:
要注册视图,请使用 InMobiNativeViewData.Builder 类:
<span class="hljs-type">InMobiNativeViewData</span> <span class="hljs-variable">nativeViewData</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiNativeViewData</span>.Builder(adViewContainer)
.setIconView(iconImageView)
.setTitleView(titleTextView)
.setDescriptionView(descriptionTextView)
.setCTAView(ctaButton)
.setRatingView(ratingBar)
.setExtraViews(Arrays.asList(sponsoredTextView))
.build();
nativeAd.registerViewForTracking(nativeViewData);
<span class="hljs-type">val</span> <span class="hljs-variable">nativeViewData</span> <span class="hljs-operator">=</span> InMobiNativeViewData.Builder(adViewContainer)
.setIconView(iconImageView)
.setTitleView(titleTextView)
.setDescriptionView(descriptionTextView)
.setCTAView(ctaButton)
.setRatingView(ratingBar)
.setExtraViews(Arrays.asList(sponsoredTextView))
.build()
nativeAd.registerViewForTracking(nativeViewData)
JSONObject getCustomAdContent()- 返回一个 JSONObject,其中包含自定义 UI 实现的附加信息(描述、图像 URL 等)。原生广告可以以三种格式嵌入到您的应用中:
例如,要在第四位展示广告:
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">AD_POSITION</span> <span class="hljs-operator">=</span> <span class="hljs-number">4</span>;
<span class="hljs-keyword">private</span> <span class="hljs-type">val</span> <span class="hljs-variable">AD_POSITION</span> <span class="hljs-operator">=</span> <span class="hljs-number">4</span>
如果广告加载成功,您将收到一个回调onAdLoadSucceeded(InMobiNative)。然后,您应该InMobiNativeAd在该监听器中将该对象添加到数据源中。如果您的应用内信息流是 `<head>` 标签,则以下是ListView一个示例实现:ListView
<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> InMobiNative nativeAd)</span> {
<span class="hljs-type">AdFeedItem</span> <span class="hljs-variable">nativeAdFeedItem</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AdFeedItem</span>(nativeAd);
mFeedItems.add(AD_POSITION, nativeAdFeedItem);
mFeedAdapter.notifyDataSetChanged();
}
<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">(nativeAd: <span class="hljs-type">InMobiNative</span>)</span></span> {
<span class="hljs-keyword">val</span> nativeAdFeedItem = AdFeedItem(nativeAd)
mFeedItems.add(AD_POSITION, nativeAdFeedItem)
mFeedAdapter.notifyDataSetChanged()
}
AdFeedItem是你的类的扩展类FeedItem,其中FeedItem是为你的每行提供支持的类ListView。此外,mItems也是ArrayList为该适配器提供支持的类ListView。目前,我们已经加载了广告并将其添加到您的数据源中。现在,是时候展示广告了。示例实现ListView如下:
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, View convertView, ViewGroup parent)</span> {
<span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">itemViewType</span> <span class="hljs-operator">=</span> getItemViewType(position);
<span class="hljs-keyword">if</span> (itemViewType == VIEW_TYPE_CONTENT_FEED) {
<span class="hljs-comment">//Your app code </span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">//Since this content type is InMobi Ad, you need to get native Ad view</span>
<span class="hljs-keyword">final</span> <span class="hljs-type">InMobiNative</span> <span class="hljs-variable">nativeAd</span> <span class="hljs-operator">=</span> ((AdFeedItem) feedItem).mNativeAd;
parent, parent.getWidth());
<span class="hljs-keyword">if</span> (convertView == <span class="hljs-literal">null</span>) {
convertView = mLayoutInflater.inflate(R.layout.ad_item, parent, <span class="hljs-literal">false</span>);
}
<span class="hljs-comment">// Get view references</span>
<span class="hljs-type">ImageView</span> <span class="hljs-variable">icon</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adIcon);
<span class="hljs-type">TextView</span> <span class="hljs-variable">title</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adTitle);
<span class="hljs-type">TextView</span> <span class="hljs-variable">description</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adDescription);
<span class="hljs-type">Button</span> <span class="hljs-variable">cta</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adAction);
<span class="hljs-type">FrameLayout</span> <span class="hljs-variable">mediaContainer</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adContent);
<span class="hljs-type">RatingBar</span> <span class="hljs-variable">ratingBar</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adRating);
<span class="hljs-type">TextView</span> <span class="hljs-variable">sponsored</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adSponsored);
<span class="hljs-type">FrameLayout</span> <span class="hljs-variable">adChoice</span> <span class="hljs-operator">=</span> convertView.findViewById(R.id.adChoice);
<span class="hljs-comment">// Populate text views</span>
title.setText(nativeAd.getAdTitle());
description.setText(nativeAd.getAdDescription());
cta.setText(nativeAd.getCtaText());
sponsored.setText(nativeAd.getAdvertiserName());
<span class="hljs-comment">// Load icon using your preferred image loading library (e.g., Picasso, Glide)</span>
<span class="hljs-keyword">if</span> (nativeAd.getAdIcon() != <span class="hljs-literal">null</span>) {
Picasso.get().load(nativeAd.getAdIcon().getUrl()).into(icon);
}
<span class="hljs-comment">// Add media view</span>
mediaContainer.removeAllViews();
<span class="hljs-keyword">if</span> (nativeAd.getMediaView() != <span class="hljs-literal">null</span>) {
<span class="hljs-comment">// Remove from previous parent if attached</span>
<span class="hljs-type">ViewGroup</span> <span class="hljs-variable">mediaParent</span> <span class="hljs-operator">=</span> (ViewGroup) nativeAd.getMediaView().getParent();
<span class="hljs-keyword">if</span> (mediaParent != <span class="hljs-literal">null</span>) {
mediaParent.removeAllViews();
}
mediaContainer.addView(nativeAd.getMediaView());
}
<span class="hljs-comment">// Set rating if available</span>
<span class="hljs-type">float</span> <span class="hljs-variable">rating</span> <span class="hljs-operator">=</span> nativeAd.getAdRating();
<span class="hljs-keyword">if</span> (rating > <span class="hljs-number">0</span>) {
ratingBar.setRating(rating);
ratingBar.setVisibility(View.VISIBLE);
} <span class="hljs-keyword">else</span> {
ratingBar.setVisibility(View.GONE);
}
<span class="hljs-comment">// Add AdChoice icon</span>
adChoice.removeAllViews();
<span class="hljs-keyword">if</span> (nativeAd.getAdChoiceIcon() != <span class="hljs-literal">null</span>) {
<span class="hljs-type">ViewGroup</span> <span class="hljs-variable">adChoiceParent</span> <span class="hljs-operator">=</span> (ViewGroup) nativeAd.getAdChoiceIcon().getParent();
<span class="hljs-keyword">if</span> (adChoiceParent != <span class="hljs-literal">null</span>) {
adChoiceParent.removeAllViews();
}
adChoice.addView(nativeAd.getAdChoiceIcon());
}
<span class="hljs-comment">// CRITICAL: Register views for tracking (Native 2.0)</span>
<span class="hljs-type">InMobiNativeViewData</span> <span class="hljs-variable">nativeViewData</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiNativeViewData</span>.Builder((ViewGroup) convertView)
.setIconView(icon)
.setTitleView(title)
.setDescriptionView(description)
.setCTAView(cta)
.setRatingView(ratingBar)
.setExtraViews(Arrays.asList(sponsored))
.build();
nativeAd.registerViewForTracking(nativeViewData);
<span class="hljs-keyword">return</span> convertView;
}
}
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">getView</span><span class="hljs-params">(position: <span class="hljs-type">Int</span>, convertViewParam: <span class="hljs-type">View</span>?, parent: <span class="hljs-type">ViewGroup</span>)</span></span>: View {
<span class="hljs-keyword">val</span> itemViewType = getItemViewType(position)
<span class="hljs-keyword">if</span> (itemViewType == VIEW_TYPE_CONTENT_FEED) {
<span class="hljs-comment">// Your app code</span>
<span class="hljs-keyword">return</span> convertViewParam ?: View(parent.context) <span class="hljs-comment">// Placeholder return, adjust as needed</span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// Since this content type is InMobi Ad, you need to get native Ad view</span>
<span class="hljs-keyword">val</span> feedItem = getItem(position) <span class="hljs-comment">// Assuming getItem method exists and returns the feed item</span>
<span class="hljs-keyword">val</span> nativeAd = (feedItem <span class="hljs-keyword">as</span> AdFeedItem).mNativeAd
<span class="hljs-keyword">var</span> convertView = convertViewParam
<span class="hljs-keyword">if</span> (convertView == <span class="hljs-literal">null</span>) {
convertView = mLayoutInflater.inflate(R.layout.ad_item, parent, <span class="hljs-literal">false</span>)
}
<span class="hljs-comment">// Get view references</span>
<span class="hljs-keyword">val</span> icon = convertView.findViewById(R.id.adIcon)
<span class="hljs-keyword">val</span> title = convertView.findViewById(R.id.adTitle)
<span class="hljs-keyword">val</span> description = convertView.findViewById(R.id.adDescription)
<span class="hljs-keyword">val</span> cta = convertView.findViewById(R.id.adAction)
<span class="hljs-keyword">val</span> mediaContainer = convertView.findViewById(R.id.adContent)
<span class="hljs-keyword">val</span> ratingBar = convertView.findViewById(R.id.adRating)
<span class="hljs-keyword">val</span> sponsored = convertView.findViewById(R.id.adSponsored)
<span class="hljs-keyword">val</span> adChoice = convertView.findViewById(R.id.adChoice)
<span class="hljs-comment">// Populate text views</span>
title.text = nativeAd.adTitle
description.text = nativeAd.adDescription
cta.text = nativeAd.ctaText
sponsored.text = nativeAd.advertiserName
<span class="hljs-comment">// Load icon using your preferred image loading library (e.g., Picasso, Glide)</span>
nativeAd.adIcon?.url?.let { url ->
Picasso.<span class="hljs-keyword">get</span>().load(url).into(icon)
}
<span class="hljs-comment">// Add media view</span>
mediaContainer.removeAllViews()
nativeAd.mediaView?.let { mediaView ->
(mediaView.parent <span class="hljs-keyword">as</span>? ViewGroup)?.removeAllViews()
mediaContainer.addView(mediaView)
}
<span class="hljs-comment">// Set rating if available</span>
<span class="hljs-keyword">val</span> rating = nativeAd.adRating
<span class="hljs-keyword">if</span> (rating > <span class="hljs-number">0</span>) {
ratingBar.rating = rating
ratingBar.visibility = View.VISIBLE
} <span class="hljs-keyword">else</span> {
ratingBar.visibility = View.GONE
}
<span class="hljs-comment">// Add AdChoice icon</span>
adChoice.removeAllViews()
nativeAd.adChoiceIcon?.let { adChoiceIcon ->
(adChoiceIcon.parent <span class="hljs-keyword">as</span>? ViewGroup)?.removeAllViews()
adChoice.addView(adChoiceIcon)
}
<span class="hljs-comment">// CRITICAL: Register views for tracking (Native 2.0)</span>
<span class="hljs-keyword">val</span> nativeViewData = InMobiNativeViewData.Builder(convertView <span class="hljs-keyword">as</span> ViewGroup)
.setIconView(icon)
.setTitleView(title)
.setDescriptionView(description)
.setCTAView(cta)
.setRatingView(ratingBar)
.setExtraViews(listOf(sponsored))
.build()
nativeAd.registerViewForTracking(nativeViewData)
<span class="hljs-keyword">return</span> convertView
}
}
该函数getItemViewType(position)检查内容类型是 FEED 还是 AD,如下所示:
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getItemViewType</span><span class="hljs-params">(<span class="hljs-type">int</span> position)</span> {
<span class="hljs-type">FeedItem</span> <span class="hljs-variable">feedItem</span> <span class="hljs-operator">=</span> getItem(position);
<span class="hljs-keyword">if</span> (feedItem <span class="hljs-keyword">instanceof</span> AdFeedItem) {
<span class="hljs-keyword">return</span> VIEW_TYPE_INMOBI_STRAND;
}
<span class="hljs-keyword">return</span> VIEW_TYPE_CONTENT_FEED;
}
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">getItemViewType</span><span class="hljs-params">(position: <span class="hljs-type">Int</span>)</span></span>: <span class="hljs-built_in">Int</span> {
<span class="hljs-keyword">val</span> feedItem: FeedItem = getItem(position)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> (feedItem <span class="hljs-keyword">is</span> AdFeedItem) {
VIEW_TYPE_INMOBI_STRAND
} <span class="hljs-keyword">else</span> VIEW_TYPE_CONTENT_FEED
}
可以使用相同的 InMobiNative 类来实现启动画面体验。但是,有几点需要注意:
InMobiNativeAd.isReady()函数来判断广告是否可以展示。有时,您的主线程可能被占用,因此您可能无法onAdLoadSucceeded及时收到通知。所以,最好在截止时间过后主动检查广告是否已准备就绪。onAdFullScreenDisplayed如果此回调被触发,则不应关闭广告。以下是一个示例实现:
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdFullScreenDisplayed</span><span class="hljs-params">(InMobiNative nativeAd)</span> {
isSecondScreenDisplayed = YES;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">dismissAd</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">if</span> (isSecondScreenDisplayed) {
Log.d(TAG, <span class="hljs-string">"DO NOT DISMISS THE AD WHILE THE SCREEN IS BEING DISPLAYED"</span>);
} <span class="hljs-keyword">else</span> {
splashAdView.setVisibility(View.GONE);
nativeAd.destroy();
}
}
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFullScreenDisplayed</span><span class="hljs-params">(nativeAd: <span class="hljs-type">InMobiNative</span>?)</span></span> {
isSecondScreenDisplayed = YES
}
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">dismissAd</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">if</span> (isSecondScreenDisplayed) {
Log.d(TAG, <span class="hljs-string">"DO NOT DISMISS THE AD WHILE THE SCREEN IS BEING DISPLAYED"</span>)
} <span class="hljs-keyword">else</span> {
splashAdView.setVisibility(View.GONE)
nativeAd.destroy()
}
}
可以使用同一个类来实现前置视频广告体验InMobiNative。您需要注册该功能videoEventListener,并在以下回调函数中关闭广告展示:
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onVideoCompleted</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiNative nativeAd)</span> {
Log.d (TAG, <span class="hljs-string">"Media playback complete "</span> + mPosition);
}
<span class="hljs-selector-tag">override</span> <span class="hljs-selector-tag">fun</span> <span class="hljs-selector-tag">onVideoCompleted</span>(<span class="hljs-variable">@NonNull</span> <span class="hljs-attribute">nativeAd</span>: InMobiNative?) {
<span class="hljs-selector-tag">Log</span><span class="hljs-selector-class">.d</span>(TAG, <span class="hljs-string">"Media playback complete $mPosition"</span>)
}
可以通过以下方式关闭前置广告:
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">dismissAd</span><span class="hljs-params">()</span> {
plashAdView.setVisibility(View.GONE);
nativeAd.destroy();
}
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">dismissAd</span><span class="hljs-params">()</span></span> {
plashAdView.setVisibility(View.GONE)
nativeAd.destroy()
}
<span class="hljs-selector-tag">public</span> <span class="hljs-selector-tag">void</span> <span class="hljs-selector-tag">onAdLoadSucceeded</span>(<span class="hljs-variable">@NonNull</span> <inmobi_ad_object> ad, <span class="hljs-variable">@NonNull</span> AdMetaInfo info) { <span class="hljs-selector-tag">Log</span><span class="hljs-selector-class">.d</span>(TAG, “Creative ID for <span class="hljs-attribute">ad </span>: “ + info.<span class="hljs-built_in">getCreativeID</span>()); }
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdLoadSucceeded</span><span class="hljs-params">(ad:<<span class="hljs-type">inmobi_ad_object</span>> , info:<span class="hljs-type">AdMetaInfo</span> )</span></span> {
Log.d(TAG, “Creative ID <span class="hljs-keyword">for</span> ad : “ + info.getCreativeID())
}
为了优化广告对用户的曝光度,您需要在不同时间点刷新广告。要刷新广告,您需要按以下顺序调用相应函数。
在刷新之前,务必adFeedItem从数据源中移除并销毁该对象。然后,您需要创建一个新对象并调用 load 方法,如下面的代码所示:
<span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">refreshAd</span><span class="hljs-params">()</span> {
Iterator < FeedItems > feedItemIterator = mFeedItems.iterator();
<span class="hljs-keyword">while</span> (feedItemIterator.hasNext()) {
<span class="hljs-keyword">final</span> <span class="hljs-type">FeedItem</span> <span class="hljs-variable">feedItem</span> <span class="hljs-operator">=</span> feedItemIterator.next();
<span class="hljs-keyword">if</span> (feedItem <span class="hljs-keyword">instanceof</span> AdFeedItem) {
feedItemIterator.remove();
}
}
<span class="hljs-comment">// refresh feed </span>
mFeedAdapter.notifyDataSetChanged();
<span class="hljs-comment">// destroy InMobiNative object </span>
inMobiNativeAd.destroy();
<span class="hljs-comment">// create InMobiNative object again </span>
<span class="hljs-type">InMobiNative</span> <span class="hljs-variable">inMobiNativeAd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiNative</span>(
<< Activity Instance >> ,
<< Your Placement ID >> ,
<< InMobiNativeAdListener created in step <span class="hljs-number">1</span> >> );
inMobiNativeAd.load();
}
<span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">refreshAd</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">val</span> feedItemIterator = mFeedItems.iterator()
<span class="hljs-keyword">while</span> (feedItemIterator.hasNext()) {
<span class="hljs-keyword">val</span> feedItem = feedItemIterator.next()
<span class="hljs-keyword">if</span> (feedItem instanceof AdFeedItem) {
feedItemIterator.remove()
}
}
<span class="hljs-comment">// refresh feed</span>
mFeedAdapter.notifyDataSetChanged()
<span class="hljs-comment">// destroy InMobiNative object</span>
inMobiNativeAd.destroy()
<span class="hljs-comment">// create InMobiNative object again</span>
InMobiNative inMobiNativeAd = new InMobiNative(
<< Activity Instance >> ,
<< Your Placement ID >> ,
<< InMobiNativeAdListener created <span class="hljs-keyword">in</span> step <span class="hljs-number">1</span> >>
);
inMobiNativeAd.load()
}
InMobiNative代表一个广告。InMobiNative与广告位置数量相同的实例。InMobiNative创建实例。onCreate()onActivityCreated()InMobiNative在onDestroy()Activity 的回调函数或OnDestroyView()Fragment 的回调函数中销毁实例。InMobiNativeAd,就必须将其销毁。销毁后,所有用户与广告视图的交互都将失效InMobiNativeAd。因此,在销毁广告视图之前,务必先将其丢弃InMobiNativeAd。InMobiNativeAd当承载它们的组件(在本例中是您的 Activity)使用 destroy() API 被销毁时,所有实例也必须被销毁。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