次の方法で共有


Android カスタム アダプター

カスタム アダプターを使用すると、通常はメディエーションのために、同じデバイスにインストールされている別の SDK に SDK が呼び出されます。 このドキュメントでは、独自のカスタム アダプターを作成するために記述する必要があるコードについて説明します。

注:

ここで説明するインターフェイスは、独自の仲介アダプターを実装するために使用したインターフェイスです。

バナー

仲介バナー広告を表示するには、 と destroyの 2 つの方法で構成されるインターフェイスを実装MediatedBannerAdViewするrequestAd必要があります。

また、仲介された SDK のイベントをモバイル SDK によって認識されるイベントに変換するコールバックを実装する責任もあります。 詳細については、次の実装例を参照してください。

 /**
 * The custom adaptor for banners must implement our mobile SDK
 * interface `MediatedBannerAdView`. In this example, the class also
 * implements the `AdListener` interface from the mediated SDK,
 * in order to handle the events from the mediated SDK.
 */
public class TestMediatedBanner implements MediatedBannerAdView {
    MediatedBannerAdViewController controller = null;
    AdView bannerAd;
    
    @Override
    public View requestAd(MediatedBannerAdViewController mediatedBannerAdViewController,
                          Activity activity, String parameter,
                          String adId, int width, int height,
                          TargetingParameters targetingParameters) {

        // Here is where you are responsible for (1) instantiating the mediated SDK,
        // (2) building a request object using the elements of `targetingParameters`.
        // Note that you are responsible for ensuring that the targeting
        // parameters can be understood by the mediated SDK.  We also assume that (3)
        // you create and return a view in the mediated SDK that you can use to make
        // the ad request.

        // Next, we tell the mediated SDK's ad view to request a banner ad from their ad
        // server.  If it works, we call the `onAdLoaded` method on our controller to
        // notify it that we've successfully loaded an ad.

        controller = mediatedBannerAdViewController;

        bannerAd = new AdView(activity, new AdSize(width, height));
        bannerAd.setAdId(adId);

        // You are responsible for creating a mapping between the events
        // emitted by the mediated SDK and those understood by our mobile SDK.
        bannerAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded(AdView adView) {
                controller.onAdLoaded();
            }

            @Override
            public void onAdRequestFailed(AdView adView) {
                // If available, map the mediated SDK's failure code
                // to the appropriate Xandr `ResultCode`.  In this example
                // we just pass in ResultCode.UNABLE_TO_FILL.
                controller.onAdFailed(ResultCode.UNABLE_TO_FILL);
            }

            @Override
            public void onAdExpanded(AdView adView) {
                controller.onAdExpanded();
            }

            @Override
            public void onAdCollapsed(AdView adView) {
                controller.onAdCollapsed();
            }

            @Override
            public void onAdClicked(AdView adView) {
                controller.onAdClicked();
            }
        });

        // create a `request` for the mediated SDK from targeting parameters.

        bannerAd.loadAd(request);
        return bannerAd;
    }

    @Override
    public void destroy() {
        // Called when the mediated SDK's view is no longer being shown.
        bannerAd.destroy();
    }
}

Xandr のモバイル SDK は、サード パーティの SDK が広告の要求を開始する必要があるときに メソッドを呼び出 requestAd します。 requestAd は、サードパーティ SDK から広告を保持するビューを返します。次のパラメーターを受け取ります。

  • mBC MediatedBannerAdViewController: アダプターが SDK にイベントを送信する必要がある 。
  • activity: Activity アダプターがビュー オブジェクトをインスタンス化できるコンテキスト。
  • parameter: サーバーから渡される省略可能な不透明な文字列。これを使用して、追加のターゲット情報などの SDK 固有のパラメーターを定義できます。 この文字列の内容のエンコードは、完全にサードパーティの SDK アダプターの実装にかかります。
  • uid: この広告通話のネットワーク ID。 この ID は、モバイル SDK に対して不透明です。ID の内容とそのエンコードは、サード パーティ製 SDK の実装にかかります。 つまり、サード パーティの SDK の広告サーバーで認識される値を設定する必要があります。
  • width: この呼び出しを開始したオブジェクトで定義されている広告の MediatedBannerAdView 幅 (ピクセル単位)。
  • height: この呼び出しを開始したオブジェクトで定義されているアドバタイズメントの MediatedBannerAdView 高さ (ピクセル単位)。
  • targetingParameters TargetingParameters年齢、性別、場所など、モバイル SDK に渡されたターゲット情報を含むオブジェクト。

スポット

ユーザーに表示される前に、仲介された SDK によってスポットをプリフェッチする必要があります。 バナーと同様に、 を実装 requestAdする必要があります。 また、広告が使用可能かどうかをユーザーに知らせ、show広告を表示する を実装isReadyする必要があります。

仲介された SDK のイベントを、SDK によって認識されるイベントに変換するコールバックを実装する責任があります。 詳細については、次の実装例を参照してください。

/**
 * The custom adaptor for interstitials must implement our mobile SDK
 * interface `MediatedInterstitialAdView`. In this example, the class also
 * implements the `AdListener` interface from the mediated SDK,
 * in order to handle the events from the mediated SDK.
 */
public class TestMediatedInterstitial implements MediatedInterstitialAdView {
    MediatedInterstitialAdViewController controller = null;
    InterstitialAd interstitialAd;
    
    @Override
    public void requestAd(MediatedInterstitialAdViewController     mediatedInterstitialAdViewController,
                          Activity activity, String parameter,
                          String adId, TargetingParameters targetingParameters) {
        // Here is where you would be responsible for (1) instantiating the mediated SDK,
        // (2) building a request object using the elements of `targetingParameters`.
        // Note that you are responsible for ensuring that the targeting
        // parameters can be understood by the mediated SDK.  We also assume that (3)
        // you create and return a view in the mediated SDK that you can use to make
        // the ad request.

        // Next, we tell the mediated SDK's ad view to request an interstitial ad from
        // their ad server.  If it works, we call the `onAdLoaded` method on our
        // controller to notify it that we've successfully loaded an ad.
        
        controller = mediatedInterstitialAdViewController;
       
        interstitialAd = new InterstitialAd(activity);
        interstitialAd.setAdId(adId);

        // You are responsible for creating a mapping between the events
        // emitted by the mediated SDK and those understood by our mobile SDK.
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded(AdView adView) {
                controller.onAdLoaded();
            }

            @Override
            public void onAdRequestFailed(AdView adView) {
                // If available, map the mediated SDK's failure code
                // to ours.  In this example we just pass in
                // ResultCode.UNABLE_TO_FILL.
                controller.onAdFailed(ResultCode.UNABLE_TO_FILL);
            }

            @Override
            public void onAdExpanded(AdView adView) {
                controller.onAdExpanded();
            }

            @Override
            public void onAdCollapsed(AdView adView) {
                controller.onAdCollapsed();
            }

            @Override
            public void onAdClicked(AdView adView) {
                controller.onAdClicked();
            }
        });

        // create a `request` for the mediated SDK from targeting parameters.

        interstitialAd.loadAd(request);
    }
    // Call when the mediated SDK should display the interstitial ad
    @Override
    public void show() {
        interstitialAd.displayAd();
    }

    // Returns whether or not the mediated SDK has an interstitial ad
    // fetched and ready to to be shown.
    @Override
    public boolean isReady() {
        return interstitialAd.isAdReady();
    }

    @Override
    public void destroy() {
        // Called when the mediated SDK's view is no longer being shown.
        interstitialAd.destroy();
    }
}

サード パーティの SDK が広告の要求を開始する必要がある場合は、アダプターがを呼び出す requestAd 必要があります。 次の引数を受け取ります。

  • mIC MediatedInterstitialAdViewController: アダプターが SDK にイベントを送信する必要がある 。
  • activity: Activity アダプターがビュー オブジェクトをインスタンス化できるコンテキスト。
  • parameter: サーバーから渡される省略可能な不透明な文字列。これを使用して、追加のターゲット情報などの SDK 固有のパラメーターを定義できます。 この文字列の内容のエンコードは、完全にサードパーティの SDK アダプターの実装にかかります。
  • uid: この広告通話のネットワーク ID。 この ID は SDK に対して不透明です。ID の内容とそのエンコードは、サード パーティ製 SDK の実装にかかります。 つまり、パラメーターには、サード パーティ製 SDK の広告サーバーによって認識される値が設定されている必要があります。
  • targetingParameters TargetingParameters: 年齢、性別、場所など、モバイル SDK に渡されたターゲット情報を含むオブジェクト。

requestAd加えて、 インターフェイスでは MediatedInterstitialAdView 、次のメソッドを実装する必要があります。

  • isReady: このメソッドを呼び出して、ユーザーが を呼び出InterstitialAdView.isReadyしたときにスポット ビューの準備ができた場合にチェックします。
  • show: このメソッドを呼び出して、ユーザーが を呼び出 InterstitialAdView.showしたときにスポット ビューを表示します。

必要なコールバック

次に示すコールバックは、カスタム アダプターによって実装する必要があります。 これらのコールバックは、対象の広告がバナーかスポットかに関係なく必要であることに注意してください。 これらの使用例は、上記のコード サンプルで確認できます。

  • onAdLoaded: 仲介された SDK の要求が完了し、バナーが正常に返されたことを示すために呼び出されます。
  • onAdFailed: 仲介された SDK の要求が有効な広告を返さなくしたことを示すために呼び出されます。

省略可能なコールバック

次に示すコールバックは、バナーとスポットの両方のカスタム アダプターに実装する必要があります。

  • onAdClicked: 広告がクリックされました。
  • onAdExpanded: 広告がモーダル ビューを表示しています。
  • onAdCollapsed: 広告がモーダル ビューを無視しています。