# 迁移到 Android 横幅广告单元 API

> 通过初始化 SDK、使用指定的广告单元 ID 创建横幅广告对象、设置自适应或固定的横幅大小以及实现监听器来管理广告事件（例如加载、显示和刷新）来集成 LevelPlay 横幅广告单元 API。

本指南介绍如何从当前实现过过渡 LevelPlay Banner API（使用广告单元 ID），以加载和显示横幅广告服务。

## 先决条件##prerequisites

* 支持的最小 SDK 为 8.6.0。您可以在[此处](/grow/levelplay/sdk/android/sdk-integration.md)下载最新的 SDK。
* 确保使用 LevelPlay Initialization API 初始化 SDK。
* 在 LevelPlay 后台中找到 AdUnitID。

## 创建横幅广告对象并设置大小##create-banner-ad-object-and-set-size

横幅广告对象的创建应在接收 **onInitSuccess** 回调后完成。

```java
// Create the banner view and set the ad unit id
LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId");
```

```java
// Create the banner view and set the ad unit id
val levelPlayBanner = LevelPlayBannerAdView(context, "adUnitId")
```

### 横幅大小##banner-sizes

| **旧版**           | **Ad Unit（广告单元）（新）** | **dp 中的尺寸**               |
| ---------------- | -------------------- | ------------------------- |
| **ISBannerSize** | **LevelPlayAdSize**  | **（Width X Height）**      |
| BANNER           | BANNER               | 320 x 50                  |
| 大型               | 大型                   | 728 x 90                  |
| 矩形               | 中矩形                  | 300 x 250                 |
| SMART            | 替换为自适应广告大小（见下文）      | 自动渲染广告服务以调整移动端和平板电脑的大小和方向 |

要创建广告大小，请遵循以下选项之一： 

\*\*适应屏幕宽度的自适应广告大小（推荐）：\*\*此选项根据设备类型返回 BANNER 或 LEADERBOARD。支持自适应功能的网络（Google、Yandex）将根据其优化逻辑返回高度。

```java
 LevelPlayAdSize adSize = LevelPlayAdSize.createAdaptiveAdSize(context);
```

```java
 val adSize = createAdaptiveAdSize(context)
```

\*\*指定的横幅大小：\*\*此选项允许您专门设置横幅大小：横幅、大型、中等矩形。

```java
levelPlayBanner.setAdSize(LevelPlayAdSize.MEDIUM_RECTANGLE);
```

```java
levelPlayBanner.setAdSize(LevelPlayAdSize.MEDIUM_RECTANGLE) 
```

### 广告位##placements

我们支持在横幅广告中[放置](/grow/levelplay/platform/settings/placements.md)仅用于举报。应在 **loadAd** 之前设置广告位，以影响所有重新加载的广告服务。

```java
// Set the placement name
levelPlayBanner.setPlacementName("placementName");
```

```java
// Set the placement name
levelPlayBanner.setPlacementName("placementName")
```

## 设置横幅监听器##set-banner-listener

在代码中实现 **LevelPlayBannerAdViewListener**，而不是监听器 **LevelPlayBannerListener**。

* 建议在加载横幅广告之前设置监听器。
* 每个横幅广告都应有自己的监听器实现。
* 回调在主线程上运行。

```java
levelPlayBanner.setBannerListener(new LevelPlayBannerAdViewListener() {
    @Override
    public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {
    // Ad was loaded successfully 
    }
    @Override
    public void onAdLoadFailed(@NonNull LevelPlayAdError error) {
    // Ad load failed
    }
    @Override
    public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {
    // Ad was displayed and visible on screen
    }
    @Override
    public void onAdDisplayFailed(@NonNull LevelPlayAdInfo adInfo, @NonNull LevelPlayAdError error) {
    // Ad failed to be displayed on screen
    }
    @Override
    public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {
    // Ad was clicked
    }
    @Override
    public void onAdExpanded(@NonNull LevelPlayAdInfo adInfo) {
    // Ad is opened on full screen
    }
    @Override
    public void onAdCollapsed(@NonNull LevelPlayAdInfo adInfo) {
    // Ad is restored to its original size
    }
    @Override
    public void onAdLeftApplication(@NonNull LevelPlayAdInfo adInfo) 
    // User pressed on the ad and was navigated out of the app 
    }
);
```

```java
levelPlayBanner.setBannerListener(object:LevelPlayBannerAdViewListener {
    覆盖 fun onAdLoaded(adInfo:LevelPlayAdInfo) {
    // Ad was loaded successfully 
    }
    覆盖 fun onAdLoadFailed（报错：LevelPlayAdError) {
    // Ad load failed
    }
    覆盖 fun onAdDisplayed(adInfo:LevelPlayAdInfo) {
    // Ad was displayed and visible on screen
    }
    覆盖 fun onAdDisplayFailed(adInfo:LevelPlayAdInfo，报错：LevelPlayAdError) {
    // Ad failed to be displayed on screen
    }
    覆盖 fun onAdClicked(adInfo:LevelPlayAdInfo) {
    // Ad was clicked
    }
    覆盖 fun onAdExpanded(adInfo:LevelPlayAdInfo) {
    // Ad is opened on full screen
    }
    覆盖 fun onAdCollapsed(adInfo:LevelPlayAdInfo) {
    // Ad is restored to its original size
    }
    覆盖 fun onAdLeftApplication(adInfo:LevelPlayAdInfo) {
    // User pressed on the ad and was navigated out of the app 
    }
})
```

|     | 旧版                      | 广告单元（新）                       |
| --- | ----------------------- | ----------------------------- |
| 监听器 | LevelPlayBannerListener | LevelPlayBannerAdViewListener |
| 回调  | onAdLoaded              | onAdLoaded                    |
|     | onAdLoadFailed          | onAdLoadFailed                |
|     | onAdClicked             | onAdClicked                   |
|     | onAdScreenPresented     | onAdExpanded                  |
|     | onAdScreenDismissed     | onAdCollapsed                 |
|     | onAdLeftApplication     | onAdLeftApplication           |
|     |                         | onAdDisplayed                 |
|     |                         | onAdDisplayFailed             |

### LevelPlay 广告信息##levelplay-ad-info

横幅监听器回调返回的 **AdInfo** 类已被 **LevelPlayAdInfo** 替换。

## 加载横幅广告##load-banner-ad

要加载横幅广告，请使用 **loadAd** 而不是 **IronSource.loadBanner**。

```java
// Load the banner ad
levelPlayBanner.loadAd();
```

```java
// Load the banner ad
levelPlayBanner.loadAd()
```

## 暂停和恢复横幅刷新##pause-and-resume-banner-refresh

如果在平台中定义了刷新值，则可以在代码中暂停横幅刷新。使用以下方法可停止横幅广告的自动刷新，或在暂停后重新启用。 

> **Note:**
>
> 再次显示横幅时，它将完成从暂停时间到刷新的时间。 

* **pauseAutoRefresh** - 暂停横幅广告的自动刷新。
* **resumeAutoRefresh** - 暂停横幅广告后恢复自动刷新。

```java
// Pause refresh
levelPlayBanner.pauseAutoRefresh();
// Resume refresh
levelPlayBanner.resumeAutoRefresh();
```

```java
// Pause refresh
levelPlayBanner.pauseAutoRefresh()
// Resume refresh
levelPlayBanner.resumeAutoRefresh()
```

## 销毁横幅广告##destroy-banner-ad

要销毁横幅，请调用 **destroy** 方法而不是 **IronSource.destroyBanner**。

已销毁的横幅无法再次显示。要显示更多广告服务，请创建一个新的 **LevelPlayBannerAdView** 对象。 

```java
levelPlayBanner.destroy()
```

```java
levelPlayBanner.destroy();
```

## 多个广告单元横幅 API##multiple-ad-unit-banner-apis

|     | 旧版                     | 广告单元（新）                   |
| --- | ---------------------- | ------------------------- |
| 横布局 | IronSourceBannerLayout | LevelPlayBannerAdView     |
| API | loadBanner             | loadAd                    |
|     | destroyBanner          | destroy                   |
|     | loadBanner             | setPlacementName          |
|     | -                      | LevelPlayAdSize.getWidth  |
|     | -                      | LevelPlayAdSize.getHeight |
|     | -                      | pauseAutoRefresh          |
|     | -                      | resumeAutoRefresh         |

## 横幅广告服务的完整实现示例##full-implementation-example-of-banner-ads

以下是使用自适应横幅大小创建和加载横幅广告的示例。

```java
public void createAndLoadBanner() {
    // Create the banner view and set the ad unit id
    LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId");
    
    // Set the placement name (optional)
    levelPlayBanner.setPlacementName("placementName");
    
    // Create the adaptive ad size to support both adaptive, banner and leaderboard (recommended)  
    LevelPlayAdSize adSize = LevelPlayAdSize.createAdaptiveAdSize(context);
    
    // Required when using createAdaptiveAdSize()
    if (adSize != null) {
        levelPlayBanner.setAdSize(adSize);
    }
    
    // Add the banner view to the container
    ViewGroup adContainer = findViewById(android.R.id.adContainer);
    adContainer.addView(levelPlayBanner);
    
    // Load the banner ad
    levelPlayBanner.loadAd();
    
    // To get actual banner layout size (either custom/standard size or adaptive)
    height = adSize.getHeight();
    width = adSize.getWidth();
}
```

```java
fun createAndLoadBanner() {
    // Create the banner view and set the ad unit id
    val levelPlayBanner = LevelPlayBannerAdView(context, "adUnitId")
    
    // Set the placement name
    levelPlayBanner.setPlacementName("placementName")
    
    // Create the adaptive ad size to support both adaptive, banner and leaderboard
    val adSize = createAdaptiveAdSize(context)
    
    // Required when using createAdaptiveAdSize()
    adSize?.let { levelPlayBanner.setAdSize(it) }
    
    // Add the banner view to the container
    val adContainer = findViewById<ViewGroup>(R.id.adContainer)
    adContainer.addView(levelPlayBanner)
    
    // Load the banner ad
    levelPlayBanner.loadAd()
    
    // Get width and height for the container
    val width = adSize?.getWidth()
    val height = adSize?.getHeight()
}
```

## LevelPlay Mediation 演示应用程序##levelplay-mediation-demo-app

Integration Demo 应用程序演示如何在 app 中集成横幅广告单元 API。

[下载 Android Demo 应用程序](https://github.com/ironsource-mobile/Mediation-Demo-Apps)
