Android でバナーをネイティブに表示する
注:
このオファリングは現在アルファ版であり、変更される可能性があります。
"バナー ネイティブ" という用語は、ネイティブ需要を返す Mobile SDK Banner クラスの機能を指します。 BannerAdView オブジェクトはマルチフォーマットであり、バナー広告の需要を返し、以下の例で説明するように API を使用して有効にした場合は、ビデオ広告とネイティブ広告です。 つまり、単一の Banner クラス オブジェクトをインスタンス化し、1 つのメンバー ID とコード (または配置 ID) を渡し、バナー、ビデオ、ネイティブメディアの種類の任意の形式またはすべての形式にアクセスする必要があります。
バナー ネイティブ機能を使用しないことにした場合、既存の実装が機能する方法に大きな変更が加えられたことに気付くことはありません。 標準の MobileSDK ヘッダーには軽微な変更があります。 ただし、コンパイル プロセスを満たすための軽微な 1 回限りの変更以外に、Banner クラスを使用する既存のコードに影響を与える必要はありません。
このドキュメントの前提は、バナー クラスとネイティブ クラスの両方について既に理解していることです。 バナー ネイティブを使用するには、構造的にも手続き的にも多くの重要な変更を管理する必要があります。 Banner Native の Banner クラスは、従来の Native Request クラスと同様に動作します。 Banner クラスは、従来の Native Request と同様にネイティブ広告オブジェクトを使用してを返すと、Native Response オブジェクトを返します。 この Native Response オブジェクトは、従来の方法で使用して、ネイティブ広告オブジェクトの広告コンテンツを表示および追跡できます。
変更の処理
MobileSDK バージョン 4.9 for Android の時点で、バナー クラスを使用してバナーとビデオ広告オブジェクトを処理する手順に破壊的変更が 1 つあります。 バナー ネイティブの導入前は、クラス インスタンスが広告オブジェクトを返す前であっても、Banner クラス インスタンス ビューをビュー階層に追加することが可能でした。 これにより、広告オブジェクトの読み込みとフェッチのライフサイクルが完了する前でも、クラスの表示設定を完了することが可能になりました。 この便利なショートカットは、広告が読み込まれたことを示すコールバックを無視する可能性があることを意味しましたが、MobileSDK の一般的なパターンです。
バナー クラスを使用してネイティブ広告をフェッチする場合、表示手順はコールバックが発生するまで待機する必要があります。 これは、コールバックがネイティブ広告オブジェクトの配信ポイントを提供し、返された場合は、ネイティブ広告オブジェクトとバナーまたはビデオ広告オブジェクトのリターンを区別する唯一の場所であるためです。
Mobile SDK の構造
AdListener は、 onAdLoaded
メソッド (NativeAdResponse 応答) を定義します。これは、使用されていない場合でも宣言する必要があります。
次の手順に従って、マルチフォーマット BannerAdView
の使用方法を理解してください。
使用と
enableNativeRendering(boolean enabled)
setAllowNativeDemand(boolean enabled)
メソッドのネイティブ需要をBannerAdView
有効にします。 ビデオ需要を有効にするには、メソッドを使用setAllowVideoDemand(boolean enabled)
します。 既定では、これらの値は false です。この場合BannerAdView
、従来の HTML バナー広告のみが配信されます。 アプリでネイティブとビデオを明示的に有効にする必要があります。/** * Sets whether or not Native Ads(AppNexus Media Type:12) should be Renderered or not. * * @param enabled whether to enable Native Assembly Renderer or not. default is false */ public void enableNativeRendering(boolean enabled) { enableNativeRendering = enabled; } /** * Sets whether or not Native Ads(AppNexus Media Type:12) can serve on this Ad object. * This overrides the value set in console. * * @param enabled whether to enable Native Ads or not. default is false */ public void setAllowNativeDemand(boolean enabled) { Clog.d(Clog.publicFunctionsLogTag, Clog.getString( R.string.set_allow_native, enabled)); requestParameters.setBannerNativeEnabled(enabled); } /** * Sets whether or not Video Ads(AppNexus Media Type:4) can serve on this Ad object. * This overrides the value set in console. * * @param enabled whether to enable Video Ads or not. default is false */ public void setAllowVideoDemand(boolean enabled) { Clog.d(Clog.publicFunctionsLogTag, Clog.getString( R.string.set_allow_video, enabled)); requestParameters.setBannerVideoEnabled(enabled); }
一方
loadAd
、できるだけ早く呼び出すことができますが、オーバーロードされた onAdLoaded 関数のいずれかが呼び出されるまで、バナー (またはビデオ) 広告をサブビューに配置することはできません。 これは、最初に返される広告のメディアの種類を決定する必要があるためです。NativeAdResponse
はオーバーロードされたonAdLoaded
呼び出しの 1 つとして返されます。これは、 によってNativeAdRequest
従来の方法で返された場合と同じ方法で処理できます。 この場合、BannerAdView
実質的には とNativeAdRequest
同じ役割を果たします。 (NativeAdResponse の詳細については、「 Android でネイティブ広告を表示 する」を参照してください)。既定では、mainImage と iconImage は読み込まれません。 これらのイメージは、 クラス
NativeAdReponse
の getImageUrl() メソッドと getIconUrl() によって返される URL を使用して手動でダウンロードできます。
例
複数形式の BannerAdView クラスの使用法は、単にバナーと従来のネイティブ クラスの使用法の組み合わせです。
この SDK を採用してもネイティブの要求をフェッチしたくない場合でも、NativeAdResponse を返すオーバーロードされたメソッドを onAdLoaded に実装する必要がありますが、空のままでもかまいません。
ネイティブまたはバナー、ビデオ、ネイティブの組み合わせをフェッチする場合は、次のコード例を使用して開始できます。
注:
ベスト プラクティスとして:
- すべての SDK メソッドは、メイン スレッドで呼び出す必要があります。
activityOnDestroy()
は、破棄されると予想される BannerAdView に対して呼び出す必要があります。
// Android: Java to handle a banner-native ad response
package com.appnexus.opensdk;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.appnexus.opensdk.*;
import com.appnexus.opensdk.R.*;
public class PresentBannerNative extends Activity {
FrameLayout adFrame;
Context context;
BannerAdView bav;
NativeAdResponse nativeAdResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
adFrame = (FrameLayout) findViewById(android.R.id.content);
// Create the AdView and set its placement ID. Tweak a few other settings.
bav = new BannerAdView(this);
bav.setPlacementID("1326299");
bav.setAdSize(300, 50);
bav.setAdListener(adListener);
bav.setAllowNativeDemand(true);
//If auto-refresh is enabled
bav.loadAd();
}
final NativeAdEventListener nativeAdEventListener = new NativeAdEventListener() {
@Override
public void onAdWasClicked() {
}
@Override
public void onAdWillLeaveApplication() {
}
};
final AdListener adListener = new AdListener() {
@Override
public void onAdRequestFailed(AdView adView, ResultCode errorCode) {
}
@Override
public void onAdLoaded(AdView adView) {
if (nativeAdResponse != null) {
// It's your responsibility to keep a reference to the view
// and `NativeAdResponse` object if necessary.
// Once done with the native ad view, call the following method to
// unregister that view.
NativeAdSDK.unRegisterTracking(adFrame);
nativeAdResponse = null;
}
adFrame.removeAllViews();
adFrame.addView(bav);
}
@Override
public void onAdLoaded(NativeAdResponse response) {
nativeAdResponse = response;
adFrame.removeAllViews();
// Title
TextView title = new TextView(context);
title.setText(nativeAdResponse.getTitle());
// Main text
TextView description = new TextView(context);
description.setText(nativeAdResponse.getDescription());
// Cover image url
String imageUrl = nativeAdResponse.getImageUrl();
// Icon image url
String iconUrl = nativeAdResponse.getIconUrl();
// Text that indicates a call to action -- for example, to install an app
TextView callToAction = new TextView(context);
callToAction.setText(nativeAdResponse.getCallToAction());
// Create a container (a parent view that holds all the views for native ads)
LinearLayout container = new LinearLayout(context);
container.addView(title);
container.addView(description);
container.addView(imageView);
container.addView(iconView);
container.addView(callToAction);
// Add the native ad container to the view hierarchy
adFrame.addView(container);
// Register native views for click and impression tracking. The
// `adEventListener` is the listener created above; it can be `null` if
// you don't want to receive notifications about click events.
// Impressions and clicks won't be counted if the view is not registered.
NativeAdSDK.registerTracking(nativeAdResponse, adFrame, nativeAdEventListener);
}
@Override
public void onAdExpanded(AdView adView) {
}
@Override
public void onAdCollapsed(AdView adView) {
}
@Override
public void onAdClicked(AdView adView) {
}
};
@Override
protected void onDestroy() {
if (bav != null) {
bav.activityOnDestroy();
}
super.onDestroy();
if (nativeAdResponse != null) {
// It's your responsibility to keep a reference to the view
// and `NativeAdResponse` object if necessary.
// Once done with the native ad view, call the following method to
// unregister that view.
NativeAdSDK.unRegisterTracking(adFrame);
nativeAdResponse = null;
}
}
}