إضافة الإعلانات الخلالية إلى تطبيق Android

تتيح لك Audience Network إمكانية تحقيق أرباح من تطبيقات Android من خلال إعلانات فيسبوك. ويُعد الإعلان الخلالي إعلانًا بملء الشاشة يمكنك عرضه في تطبيقك. بصفة أساسية، يتم عرض الإعلانات الخلالية عندما يحدث انتقال في تطبيقك. على سبيل المثال، بعد إنهاء مستوى في لعبة أو بعد تحميل حدث في تطبيق أخبار.

احرص على قراءة دليل بدء استخدام Audience Network ودليل بدء استخدام Android قبل المتابعة.

الخطوات التفصيلية

الخطوة الأولى: تهيئة الإعلانات الخلالية في نشاطك

الخطوة الثانية: عرض الإعلانات الخلالية في نشاطك

تهيئة مجموعة Audience Network SDK

تمت إضافة هذا الأسلوب في مجموعة Audience Network SDK لنظام Android بالإصدار 5.1.

يلزم وجود تهيئة واضحة لمجموعة Audience Network SDK لنظام Android بالإصدار 5.3.0 والإصدارات الأحدث. يرجى الرجوع إلى هذا المستند حول كيفية تهيئة مجموعة Audience Network SDK لنظام Android.

قبل إنشاء كائن إعلان وتحميل إعلانات، يجب عليك تهيئة مجموعة Audience Network SDK. ويوصى بإجراء ذلك عند تشغيل التطبيق.

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

الخطوة الأولى: تهيئة الإعلانات الخلالية في نشاطك

أضف الرمز البرمجي التالي أعلى نشاطك لاستيراد مجموعة 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");
...  

الخطوة الثانية: عرض الإعلانات الخلالية

السيناريو الأول: أنشئ 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
    }
}

أخيرًا، أضف الرمز التالي في وظيفة onDestroy() لدى نشاطك لتوفير المصادر التي يستخدمها InterstitialAd:

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

إذا كنت تستخدم محاكي Google Android الافتراضي، فستتم إضافة سطر الرمز البرمجي التالي قبل تحميل أي إعلان اختباري:
AdSettings.addTestDevice("HASHED ID");.

استخدم معرف التجزئة المطبوع في سجل رسائل النظام "log cat" عندما تقدم طلبًا لأول مرة لتحميل إعلان على أحد الأجهزة.

لا تحتاج الأجهزة التي تستخدم Genymotion والأجهزة الفعلية إلى إجراء هذه الخطوة. إذا كنت تريد الاختبار باستخدام إعلانات حقيقية، الرجاء الرجوع إلى إرشادات الاختبار.

شغّل تطبيقك ومن المفترض أن يظهر إعلان خلالي:

تسريع الأجهزة لإعلانات الفيديو

تتطلب إعلانات الفيديو في Audience Network تمكين ميزة عرض يستند إلى تسريع الأجهزة، وإن لم يتم ذلك فقد تظهر شاشة سوداء في مشاهدات الفيديو. ينطبق ذلك على

  • تصميمات الفيديو في الإعلانات الأصلية
  • تصميمات الفيديو في الإعلانات الخلالية
  • إعلانات الفيديو المُضمن في المحتوى
  • مقاطع فيديو بمكافأة

ميزة تسريع الأجهزة بشكل افتراضي في مستوى واجهة Target 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" .../>

الخطوات التالية