# Android のバナー広告単位 API への移行

> SDK の初期化、特定の広告単位 ID を持つバナー広告オブジェクトの作成、適応型または固定型のバナーサイズの設定、ロード、表示、更新などの広告イベントを管理するリスナーの実装によって、LevelPlay バナー Ad 単位 API を統合します。

このガイドでは、現在の実装から LevelPlay バナー API (広告単位 ID を使用) に遷移し、バナー広告をロードしてディスプレイする方法について説明します。

## 前提条件##prerequisites

* サポートされている最小 SDK は 8.6.0 です。最新の SDK は[ここ](/grow/levelplay/sdk/android/sdk-integration.md)からダウンロードできます。
* LevelPlay 初期化 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

| **古い機能**         | **広告単位(新規)**                                    | **Dimensions in dp**.                  |
| ---------------- | ----------------------------------------------- | -------------------------------------- |
| **ISBannerSize** | **LevelPlayAdSize**                             | **(幅 X 高さ)**                           |
| バナー              | バナー                                             | 320×50                                 |
| 大規模              | 大規模                                             | 728 x 90                               |
| 矩形               | MEDIUM\_RECTANGLE                               | 300 x 250                              |
| スマート             | Adaptive Ad Size (適応型広告サイズ) に置き換えられました (以下を参照)。 | 広告を自動的にレンダリングして、モバイルおよびタブレットのサイズと向きを調整 |

広告サイズを作成するには、以下のいずれかのオプションに従います。 

\*\*画面幅に合わせて調整できる適応型広告サイズ（推奨）：\*\*このオプションは、デバイス タイプに応じてバナーまたは LeaderBOARD を返します。適応特徴をサポートするネットワーク (Google、Yandex) は、最適化ロジックに基づいて高ささを返します。

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

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

\*\*特定のバナーサイズ：\*\*このオプションでは、バナーサイズを明確に設定できます。バナー、LARGE、MEDIUM＿矩形。

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

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

### Placements##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

リスナーの **LevelPlayBanner**Listener の代わりに、 **LevelPlayBannerAdViewListener** をコードに実装します。

* バナー広告をロードする前にリスナーを設定することをお勧めします。
* バナー広告ごとに独自のリスナーを実装する必要があります。
* コールバックはメインスレッドで実行されます。

```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 {
    override fun onAdLoaded(adInfo:LevelPlayAdInfo) {
    // Ad was loaded successfully 
    }
    override fun onAdLoadFailed(error:LevelPlayAdError) {
    // Ad load failed
    }
    override fun onAdDisplayed(adInfo:LevelPlayAdInfo) {
    // Ad was displayed and visible on screen
    }
    override fun onAdDisplayFailed(adInfo:LevelPlayAdInfo、エラー:LevelPlayAdError) {
    // Ad failed to be displayed on screen
    }
    override fun onAdClicked(adInfo:LevelPlayAdInfo) {
    // Ad was clicked
    }
    オーバーライド fun onAdExpanded(adInfo:LevelPlayAdInfo) {
    // Ad is opened on full screen
    }
    override fun onAdCollapsed(adInfo:LevelPlayAdInfo) {
    // Ad is restored to its original size
    }
    override 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

バナー広告をロードするには、**Ironsource.loadBanner** の代わりに **loadAd** を使用します。

```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

バナーを破棄するには、 **Ironsource.destroyBanner** の代わりに **destroy** メソッドを呼び出します。

破棄されたバナーは再度表示できなくなります。さらに広告を表示するには、新しい **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

インテグレーション Demo (インテグレーションデモ) アプリケーションは、バナー広告単位 API をアプリケーションに統合する方法を示します。

[Androidデモアプリケーションのダウンロード](https://github.com/ironsource-mobile/Mediation-Demo-Apps)
