Audience Network 可讓您利用 Facebook 廣告,將 Android 應用程式變成您的獲利來源。插頁廣告是可在應用程式中顯示的全螢幕廣告,通常會在應用程式進行轉場時顯示,例如:完成遊戲關卡後,或在新聞應用程式中載入報導後。
繼續操作前,請務必詳閱 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:
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");
。
首次發出在裝置上載入廣告的要求時,請使用列印至 logcat 的雜湊編號。
Genymotion 和實體裝置無須進行這個步驟。如果想要使用真實廣告進行測試,請參閱測試指南。
啟動應用程式後,您應該就會看到插頁廣告出現:
Audience Network 中的影片廣告必須啟用硬體加速顯示,否則觀看影片時可能會出現畫面變黑的情況。適用項目
如果目標 API 層級 >=14(Ice Cream Sandwich,Android 4.0.1),預設會啟用硬體加速,但也可在應用程式層級或活動層級明確啟用這個功能。
在 Android Manifest 檔案中,將以下屬性加入 <application>
標籤,即可啟用整個應用程式的硬體加速:
<application android:hardwareAccelerated="true" ...>
如果只想針對應用程式中的特定活動啟用這個功能,可以在 Android Manifest 檔案中將以下功能加入 <activity>
標籤。以下範例會針對 AudienceNetworkActivity
啟用硬體加速,這是用於顯示插頁廣告和獎勵式影片:
<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>
參考 Github 上的 Audience Network Android 程式碼範例,將專案匯入到您的整合開發環境(IDE),然後在裝置或模擬器上執行。
當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策與 Facebook 社群守則,接著便可以將應用程式送審。