Thêm quảng cáo chèn giữa vào ứng dụng Android

Với Audience Network, bạn có thể kiếm tiền từ ứng dụng Android của mình bằng quảng cáo trên Facebook. Quảng cáo chèn giữa là quảng cáo toàn màn hình có thể hiển thị trong ứng dụng. Thông thường, quảng cáo chèn giữa hiển thị khi có sự chuyển tiếp trong ứng dụng. Ví dụ – sau khi qua một cấp độ trong game hoặc sau khi tải một tin trong ứng dụng tin tức.

Đảm bảo bạn đã hoàn tất hướng dẫn Bắt đầuBắt đầu dành cho Android với Audience Network trước khi tiếp tục.

Hướng dẫn từng bước

Bước 1: Khởi tạo quảng cáo chèn giữa trong Hoạt động của bạn

Bước 2: Hiển thị quảng cáo chèn giữa trong Hoạt động của bạn

Khởi tạo Audience Network SDK

Phương thức này được thêm vào Audience Network Android SDK phiên bản 5.1.

Khởi tạo công khai Audience Network Android SDK là yêu cầu bắt buộc đối với phiên bản 5.3.0 trở lên. Vui lòng tham khảo tài liệu này về cách khởi tạo Audience Network Android SDK.

Trước khi tạo đối tượng quảng cáo và tải quảng cáo, bạn cần khởi tạo Audience Network SDK. Bạn nên thực hiện việc này khi khởi chạy ứng dụng.

public class YourApplication extends Application {
    ...
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Audience Network SDK
        AudienceNetworkAds.initialize(this);       
    }
    ...
}

Bước 1: Khởi tạo quảng cáo chèn giữa trong Hoạt động của bạn

Thêm mã sau vào đầu Hoạt động để nhập SDK quảng cáo trên Facebook:

import com.facebook.ads.*;

Khởi tạo 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");
...  

Bước 2: Hiển thị quảng cáo chèn giữa

Tình huống 1: Tạo InterstitialAdListener, tải Quảng cáo và hiển thị ngay khi tải thành công.

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());
    }
}

Quảng cáo chèn giữa chứa nội dung có kích thước lớn hơn nên cách làm phù hợp là gọi trước loadAd(...) rồi gọi show() vào thời điểm thích hợp.

Tình huống 2: Hiển thị quảng cáo trong vài giây hoặc vài phút sau khi tải thành công. Bạn nên kiểm tra xem quảng cáo có bị vô hiệu hóa hay không trước khi hiển thị quảng cáo đó.

Trong trường hợp không hiển thị quảng cáo ngay sau khi tải quảng cáo, nhà phát triển sẽ chịu trách nhiệm kiểm tra xem quảng cáo có bị vô hiệu hóa hay không. Sau khi tải thành công, quảng cáo sẽ có hiệu lực trong 60 phút. Bạn sẽ không được thanh toán nếu hiển thị một quảng cáo không hợp lệ.

Bạn nên làm theo ý tưởng bên dưới. Tuy nhiên, vui lòng không sao chép mã vào dự án của bạn vì đây chỉ là mã ví dụ:

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
    }
}

Cuối cùng, hãy thêm mã dưới đây vào hàm onDestroy() cho Hoạt động của bạn để phát hành các tài nguyên mà InterstitialAd sử dụng:

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

Nếu đang sử dụng trình mô phỏng Google Android mặc định, thì bạn cần thêm dòng mã sau trước khi tải quảng cáo thử nghiệm:
AdSettings.addTestDevice("HASHED ID");.

Sử dụng ID đã mã hóa được in vào logcat khi bạn yêu cầu tải quảng cáo trên thiết bị lần đầu.

Các thiết bị Genymotion và thiết bị vật lý không cần bước này. Nếu bạn muốn thử nghiệm với quảng cáo thực, vui lòng tham khảo Hướng dẫn thử nghiệm của chúng tôi.

Khởi động ứng dụng và bạn sẽ thấy Quảng cáo chèn giữa xuất hiện:

Tăng tốc phần cứng cho quảng cáo video

Quảng cáo video trong Audience Network yêu cầu phải bật hiển thị được tăng tốc phần cứng, nếu không, bạn có thể gặp phải màn hình đen trong khi xem video. Điều này áp dụng với

  • Nội dung video trong quảng cáo tự nhiên
  • Nội dung video trong quảng cáo chèn giữa
  • Quảng cáo video trực tuyến
  • Video kèm phần thưởng

Tăng tốc phần cứng sẽ được bật theo mặc định nếu cấp API mục tiêu của bạn >=14 (Ice Cream Sandwich, Android 4.0.1), nhưng bạn cũng có thể bật tính năng này ở cấp ứng dụng hoặc cấp hoạt động.

Cấp độ ứng dụng

Trong tệp kê khai Android của bạn, hãy thêm thuộc tính sau vào thẻ <application> để bật tính năng tăng tốc phần cứng cho toàn bộ ứng dụng:

<application android:hardwareAccelerated="true" ...>

Cấp độ hoạt động

Nếu bạn chỉ muốn bật tính năng này cho các hoạt động cụ thể trong ứng dụng, thì trong tệp kê khai Android của mình, bạn có thể thêm tính năng sau vào thẻ <activity>. Trong ví dụ sau, chúng tôi sẽ bật tính năng tăng tốc phần cứng cho AudienceNetworkActivity dùng để hiển thị quảng cáo chèn giữa và video kèm phần thưởng:

<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>

Các bước tiếp theo