เพิ่มโฆษณาคั่นลงในแอพ Android

Audience Network ช่วยให้คุณสามารถสร้างรายได้จากแอพ Android ของคุณด้วยโฆษณาบน Facebook ได้ โฆษณาคั่นคือโฆษณาแบบเต็มหน้าจอที่คุณสามารถแสดงในแอพของคุณ โดยทั่วไปแล้ว ระบบจะแสดงโฆษณาคั่นเมื่อมีช่วงต่อระหว่างคลิปในแอพ ตัวอย่างเช่น หลังผ่านด่านในเกมหรือหลังโหลดเนื้อความในแอพข่าว

อย่าลืมดูคู่มือเริ่มต้นใช้งาน Audience Network และเริ่มต้นใช้งาน Android ก่อนดำเนินการต่อ

วิธีการทีละขั้นตอน

ขั้นตอนที่ 1: การเริ่มต้นโฆษณาคั่นในกิจกรรมของคุณ

ขั้นตอนที่ 2: การแสดงโฆษณาคั่นในกิจกรรมของคุณ

เริ่มต้น Audience Network SDK

ระบบได้เพิ่มเมธอดนี้ใน Android Audience Network SDK เวอร์ชั่น 5.1

การเริ่มต้นของ Audience Network Android SDK ที่ชัดเจนเป็นข้อบังคับตั้งแต่เวอร์ชั่น 5.3.0 ขึ้นไป โปรดดูเอกสารนี้เกี่ยวกับวิธีการเริ่มต้น 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: การเริ่มต้นโฆษณาคั่นในกิจกรรมของคุณ

เพิ่มโค้ดต่อไปนี้ไว้ที่ด้านบนสุดของกิจกรรมเพื่อนำเข้า SDK โฆษณาบน Facebook:

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

ใช้ ID แฮชซึ่งมีการพิมพ์บันทึกไว้ใน Logcat ขณะที่คุณทำการส่งคำขอเพื่อโหลดโฆษณาบนอุปกรณ์เป็นครั้งแรก

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" .../>

ขั้นตอนถัดไป