Audience Networkを利用すると、Android用アプリにFacebook広告を掲載して収益を生み出すことができます。インタースティシャル広告は、アプリ内に表示できるフルスクリーン広告です。多くの場合、インタースティシャル広告は、アプリでトランジションがある場合に表示されます。例えば、ゲームで1つのレベルが完了した後や、ニュースアプリで1つのストーリーを読み込んだ後などです。
続行する前に、必ずAudience NetworkのスタートガイドとAndroidのスタートガイドすべてに目を通しておいてください。
このメソッドは、Android Audience Network SDKバージョン5.1で追加されました。
バージョン5.3.0
以降では、Audience Network Android SDKの明示的な初期化が必須です。Audience Network Android SDKを初期化する方法については、こちらのドキュメントをご覧ください。
広告オブジェクトを作成したり、広告を読み込んだりする前に、Audience Network SDKを初期化してください。アプリの起動時に初期化することをおすすめします。
public class YourApplication extends Application { ... @Override public void onCreate() { super.onCreate(); // Initialize the Audience Network SDK AudienceNetworkAds.initialize(this); } ... }
Facebook広告SDKをインポートするには、お使いのActivityの先頭に以下のコードを追加します。
import com.facebook.ads.*;
InterstitialAd
を初期化します。
private InterstitialAd interstitialAd; @Override public void onCreate(Bundle savedInstanceState) { ... // Instantiate an InterstitialAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID"); ...
InterstitialAdListener
を作成し、広告を読み込み、読み込みが成功したらすぐに広告を表示します。public class InterstitialAdActivity extends Activity { private final String TAG = InterstitialAdActivity.class.getSimpleName(); private InterstitialAd interstitialAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Instantiate an InterstitialAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID"); // Create listeners for the Interstitial Ad InterstitialAdListener interstitialAdListener = new InterstitialAdListener() { @Override public void onInterstitialDisplayed(Ad ad) { // Interstitial ad displayed callback Log.e(TAG, "Interstitial ad displayed."); } @Override public void onInterstitialDismissed(Ad ad) { // Interstitial dismissed callback Log.e(TAG, "Interstitial ad dismissed."); } @Override public void onError(Ad ad, AdError adError) { // Ad error callback Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage()); } @Override public void onAdLoaded(Ad ad) { // Interstitial ad is loaded and ready to be displayed Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!"); // Show the ad interstitialAd.show(); } @Override public void onAdClicked(Ad ad) { // Ad clicked callback Log.d(TAG, "Interstitial ad clicked!"); } @Override public void onLoggingImpression(Ad ad) { // Ad impression logged callback Log.d(TAG, "Interstitial ad impression logged!"); } }; // For auto play video ads, it's recommended to load the ad // at least 30 seconds before it is shown interstitialAd.loadAd( interstitialAd.buildLoadAdConfig() .withAdListener(interstitialAdListener) .build()); } }
インタースティシャル広告にはサイズの大きなクリエイティブがあるため、事前にloadAd(...)
を呼び出してから、適切なタイミングでshow()
を呼び出すのは良いでしょう。
広告が読み込まれた後すぐに広告を表示しない場合、広告が無効にされたのかどうかをチェックするのは、開発者の責任です。広告が正常に読み込まれたら、広告は60分間有効になります。無効にされた広告を表示しても、収入にはなりません。
以下を参考にしてください。ただし、このコードは例に過ぎないので、そのままプロジェクトにコピーして使用しないでください。
public class InterstitialAdActivity extends Activity { private InterstitialAd interstitialAd ; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate an InterstitialAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID"); InterstitialAdListener interstitialAdListener = new InterstitialAdListener() { ... }; // load the ad interstitialAd.loadAd( interstitialAd.buildLoadAdConfig() .withAdListener(interstitialAdListener) .build()); } private void showAdWithDelay() { /** * Here is an example for displaying the ad with delay; * Please do not copy the Handler into your project */ // Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { // Check if interstitialAd has been loaded successfully if(interstitialAd == null || !interstitialAd.isAdLoaded()) { return; } // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad. if(interstitialAd.isAdInvalidated()) { return; } // Show the ad interstitialAd.show(); } }, 1000 * 60 * 15); // Show the ad after 15 minutes } }
最後に、InterstitialAd
の使うリソースを解放するため、以下のコードをActivityのonDestroy()
関数に追加します。
@Override protected void onDestroy() { if (interstitialAd != null) { interstitialAd.destroy(); } super.onDestroy(); }
デフォルトのGoogle Androidエミュレータを使用する場合は、テスト広告の読み込み前に次のコード行を追加します。AdSettings.addTestDevice("HASHED ID");
指定するIDは、デバイス上で広告を読み込むリクエストを最初に実行したときLogCatに出力される、ハッシュ化されたIDです。
Genymotionと物理的なデバイスではこのステップは必要ありません。実際の広告を使用してテストする場合は、テストガイドをご覧ください。
アプリを起動します。インタースティシャル広告が表示されるはずです。
Audience Networkの動画広告では、ハードウェアアクセラレーションを使用したレンダリングを有効にする必要があります。有効にしない場合は、動画の表示中に画面が黒くなることがあります。対象広告:
ハードウェアアクセラレーションは、ターゲットAPIレベルが14以上(Ice Cream Sandwich, Android 4.0.1)の場合はデフォルトで有効になっていますが、アプリレベルまたはアクティビティレベルで明示的に有効にすることもできます。
Androidのマニフェストファイルで、次の属性を<application>
タグに追加し、アプリ全体でハードウェアアクセラレーションを有効にします。
<application android:hardwareAccelerated="true" ...>
アプリの特定のアクティビティに対してのみこの機能を有効にする場合は、Androidのマニフェストファイルで次の機能を<activity>
タグに追加します。以下の例では、インタースティシャル広告とリワード動画のレンダリングに使用されるAudienceNetworkActivity
に対してハードウェアアクセラレーションを有効にします。
<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>
GithubでAudience Network Androidコードサンプルをご確認ください。プロジェクトをIDEにインポートして、機器またはエミュレータで実行します。
アプリを公開して収益を得る準備が整ったら、Audience NetworkのポリシーとFacebookコミュニティ規定を順守していることを確認してから、アプリのレビューを申請します。