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

您可以在 XML 布局资源文件中添加横幅广告,并像在常规视图中一样在代码中引用它。
以下代码片段展示了如何在 XML 中添加横幅广告。请注意,您需要将 InMobi 广告命名空间添加到顶级布局元素中才能添加横幅广告。
<androidx.constraintlayout.widget.constraintlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://"schemas.android.com/apk/res-auto"
xmlns:ads="http://"schemas.android.com/apk/lib/com.inmobi.ads"
xmlns:tools="http://"schemas.android.com/tools"
对于横幅广告 XML 集成,使用 Android SDK 720 及更高版本的发布商需要将“plid-”附加到placementIdTAG,如下所示:
ads:placementId="plid-1431977751489005"
现在,您可以在代码中引用该横幅广告:
InMobiBanner bannerAd = (InMobiBanner)findViewById(R.id.banner);
val bannerAd:InMobiBanner = findViewById(R.id.banner)
获得横幅实例后,您可以按照以下各节所示请求加载广告。
在此之前,请InMobiSdk.init(Context, String)在您的主活动或显示横幅广告的活动中调用该方法来初始化 InMobi SDK。
要在 Android 代码中添加横幅广告,请创建以下实例InMobiBanner:
InMobiBanner bannerAd = new InMobiBanner(BannerAdsActivity.this, 1468078426600L);
val bannerAd = InMobiBanner(this@BannerAdsActivity, 1468078426600L)
InMobiBanner 不是线程安全的。横幅实例必须在 UI 线程上创建。创建横幅广告后,您可以将其添加到视图层级结构中:
RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.ad_container);
RelativeLayout.LayoutParams bannerLp = new RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > );
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adContainer.addView(bannerAd, bannerLayoutParams);
val adContainer:RelativeLayout = findViewById(R.id.ad_container)
val bannerLp:RelativeLayout.LayoutParams = RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > )
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL)
adContainer.addView(bannerAd, bannerLayoutParams)
WRAP_CONTENT此外,将布局参数作为横幅广告的布局参数是编程错误。无论您是在 XML 布局资源文件中创建横幅广告,还是在代码中创建,此限制都适用。MATCH_PARENT将横幅广告的布局参数提供给 InMobi SDK。InMobi SDK 将正确计算横幅广告的视图尺寸,并使用这些信息从 InMobi 广告网络中获取广告。加载横幅广告需要先将横幅广告视图添加到视图层级结构中,然后再向实例请求广告InMobiBanner。将视图添加到视图层级结构后,InMobi SDK 才能正确计算横幅广告视图的尺寸,并从网络中请求与这些尺寸最匹配的广告。
将视图添加InMobiBanner到视图层次结构后,就可以调用load()刚刚创建的实例上的方法来请求广告。
首次通过调用实例请求广告后,横幅广告会自动刷新load()。InMobiBanner您可以在创建横幅广告时(在 XML 布局资源文件中)设置自动刷新,也可以稍后在代码中通过调用setRefreshInterval(int)横幅广告的相应方法来设置。
以下示例展示了如何在 XML 中实现此功能:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.inmobi.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
...
>
<com.inmobi.ads.InMobiBanner
android:layout_width="320dp"
android:layout_height="50dp"
android:id="@+id/banner"
ads:placementId="plid-1431977751489005"
ads:refreshInterval="60"
/>
</LinearLayout>
您可以选择横幅刷新时生效的动画样式。您也可以选择关闭横幅刷新时的任何动画效果。
为此,您可以setAnimationType(InMobiBanner.AnimationType)在获取横幅实例后调用该方法。
默认动画已启用AnimationType.ROTATE_HORIZONTAL_AXIS。如果要关闭此功能,请调用setAnimationType带有AnimationType.ANIMATION_OFF参数的方法。
如果您想监听横幅生命周期中的关键事件,则需要实现该BannerAdListener接口。然后,您可以使用实例setListener(BannerAdListener)上的相应方法来设置此监听器InMobiBanner。每个事件的简要说明如下。
/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
public abstract class BannerAdEventListener {
/**
* Called to notify that an ad was received successfully but is not ready to be displayed yet.
*
* @param ad Represents the ad which was loaded or preloaded
* @param info Represents the ad meta information
*/
public void onAdFetchSuccessful(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {}
/**
* Called to notify that an ad was successfully loaded.
* @param ad Represents the {@link InMobiBanner} ad which was loaded
* @param info Represents the ad meta information
*/
public void onAdLoadSucceeded(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {}
/**
* Called to notify that a request to load an ad failed.
* @param ad Represents the {@link InMobiBanner} ad which failed to load
* @param status Represents the {@link InMobiAdRequestStatus} status containing error reason
*/
public void onAdLoadFailed(@NonNull InMobiBanner ad, @NonNull InMobiAdRequestStatus status) {}
/**
* Called to notify that the user interacted with the ad.
* @param ad Represents the {@link InMobiBanner} ad on which user clicked
* @param params Represents the click parameters
*/
public void onAdClicked(@NonNull InMobiBanner ad, Map < Object, Object > params) {}
/**
* Called to notify that the banner ad was displayed
* @param ad Represents the {@link InMobiBanner} ad which was displayed
*/
public void onAdDisplayed(@NonNull InMobiBanner ad) {}
/**
* Called to notify that the User is about to return to the application after closing the ad.
* @param ad Represents the {@link InMobiBanner} ad which was closed
*/
public void onAdDismissed(@NonNull InMobiBanner ad) {}
/**
* Called to notify that the user is about to leave the application as a result of interacting with the ad.
* @param ad Represents the {@link InMobiBanner} ad
*/
public void onUserLeftApplication(@NonNull InMobiBanner ad) {}
/**
* Called to notify that a reward was unlocked.
* @param ad Represents the {@link InMobiBanner} ad for which rewards was unlocked
* @param rewards Represents the rewards unlocked
*/
public void onRewardsUnlocked(@NonNull InMobiBanner ad, Map < Object, Object > rewards) {}
/**
* Called to notify that inmobi has logged an impression for the ad
* @param ad Represents the ad which was impressed
*/
public void onAdImpression(@NonNull InMobiBanner ad) {
}
}
/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
abstract class BannerAdEventListener : AdEventListener<inmobibanner>() {
/**
* Called to notify that an ad preload has failed.
*
***Note** This notification is given only when you use `preload()` in
* [InMobiBanner.preloadManager]
*
* @param ad Represents the [InMobiBanner] ad which was preloaded
* @param status Represents the [InMobiAdRequestStatus] status containing error reason
*/
open fun onAdFetchFailed(ad: InMobiBanner, status: InMobiAdRequestStatus) {}
/**
* Called to notify that the banner ad has expanded
*
* @param ad Represents the [InMobiBanner] ad which was expanded
*/
open fun onAdDisplayed(ad: InMobiBanner) {}
/**
* Called to notify that the User is about to return to the application after closing the ad.
*
* @param ad Represents the [InMobiBanner] ad which was closed
*/
open fun onAdDismissed(ad: InMobiBanner) {}
/**
* Called to notify that the user is about to leave the application as a result of interacting with the ad.
*
* @param ad Represents the [InMobiBanner] ad
*/
open fun onUserLeftApplication(ad: InMobiBanner) {}
/**
* Called to notify that a reward was unlocked.
*
* @param ad Represents the [InMobiBanner] ad for which rewards was unlocked
* @param rewards Represents the rewards unlocked
*/
open fun onRewardsUnlocked(ad: InMobiBanner, rewards: Map<any, any="">) {}
/**
* Called to notify that inmobi has logged an impression for the ad
* @param ad Represents the ad which was impressed
*/
open fun onAdImpression(ad: InMobiBanner) {}
}
如果您已不再使用 Banner 对象,只需对其调用 deprecate 方法即可释放资源。这将执行以下操作:
bannerAd.destroy(); // bannerAd is an example instance.
bannerAd.destroy() // bannerAd is an example instance.
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.