Android 앱에 전면 광고 추가

Audience Network를 사용하면 Facebook 광고를 통해 Android 앱에서 수익을 창출할 수 있습니다. 전면 광고는 앱에 표시할 수 있는 전체 화면 광고입니다. 일반적으로 전면 광고는 앱에서 전환이 발생할 때 표시됩니다. 예를 들어 게임에서 어떠한 레벨에 달성하거나 새 앱에서 스토리를 읽어들인 후에 표시할 수 있습니다.

계속하기 전에 Audience Network 시작하기Android 시작하기 가이드를 완료했는지 확인하세요.

단계별 안내

1단계: 활동에서 전면 광고 초기화

2단계: 활동에서 전면 광고 표시

Audience Network SDK 초기화

이 메서드는 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);       
    }
    ...
}

1단계: 활동에서 전면 광고 초기화

Facebook 광고 SDK를 가져오려면 활동의 맨 위에 다음 코드를 추가합니다.

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");
...  

2단계: 전면 광고 표시

시나리오 1: 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()를 호출하는 것이 좋습니다.

시나리오 2: 광고가 성공적으로 읽어들여지면 몇 초 또는 몇 분 이내에 광고가 표시됩니다. 광고가 표시되기 전에 무효화되지 않았는지 확인해야 합니다.

광고를 읽어들인 후 즉시 광고를 표시하지 않는 경우 개발자는 광고가 무효화되지 않았는지 확인해야 합니다. 광고를 성공적으로 읽어들이고 나면 광고는 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
    }
}

마지막으로 다음 코드를 활동의 onDestroy() 함수에 추가하여 InterstitialAd가 사용하는 리소스를 릴리스합니다.

@Override
protected void onDestroy() {
    if (interstitialAd != null) {
        interstitialAd.destroy();
    }
    super.onDestroy();
}

기본 Google Android 에뮬레이터를 사용하는 경우 테스트 광고를 읽어들이기 전에 다음 코드 행을 추가합니다.
AdSettings.addTestDevice("HASHED 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" .../>

다음 단계