The Audience Network allows you to monetize your Android apps with Facebook ads. Rewarded interstitial ads are a full screen experience which starts when users reach a natural break in a game, and which offers rewards in exchange for a user viewing the full ad. Users can opt-out at any time. The ad experience is 15-30 seconds and contains an end card with a call to action. Upon completion of the full video, you will receive a callback to grant the suggested reward to the user.
確保您在開始操作前,已經先行參閱 Audience Network 新手指南及 Android 新手指南。
Android Audience Network SDK 5.1 版已加入此方法。
若是 5.3.0
或更高版本,您必須初始化 Android 版 Audience Network SDK如欲了解有關初始化 Android 版 Audience Network SDK 的方法,請參閱此文件。
在建立廣告物件及載入廣告前,請為 Audience Network SDK 執行初始化。我們建議您在啟動應用程式時執行此動作。
public class YourApplication extends Application { ... @Override public void onCreate() { super.onCreate(); // Initialize the Audience Network SDK AudienceNetworkAds.initialize(this); } ... }
Add the following code at the top of your Activity in order to import the Facebook Ads SDK:
import com.facebook.ads.*;
Then, initialize the rewarded video object, set the listener and load the video creative. The rewarded ad requires a RewardedInterstitialAdListener
interface which implements the following methods in the sample code to handle various events. For example in your activity:
private final String TAG = YourActivity.class.getSimpleName(); private RewardedInterstitialAd rewardedInterstitialAd; @Override public void onCreate(Bundle savedInstanceState) { ... // Instantiate a RewardedInterstitialAd 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). rewardedInterstitialAd = new RewardedInterstitialAd(this, "YOUR_PLACEMENT_ID"); RewardedInterstitialAdListener rewardedInterstitialAdListener = new RewardedInterstitialAdListener() { @Override public void onError(Ad ad, AdError error) { // Rewarded interstitial ad failed to load Log.e(TAG, "Rewarded interstitial ad failed to load: " + error.getErrorMessage()); } @Override public void onAdLoaded(Ad ad) { // Rewarded interstitial ad is loaded and ready to be displayed Log.d(TAG, "Rewarded interstitial ad is loaded and ready to be displayed!"); } @Override public void onAdClicked(Ad ad) { // Rewarded interstitial ad clicked Log.d(TAG, "Rewarded interstitial ad clicked!"); } @Override public void onLoggingImpression(Ad ad) { // Rewarded Interstitial ad impression - the event will fire when the // interstitial starts playing Log.d(TAG, "Rewarded interstitial ad impression logged!"); } @Override public void onRewardedInterstitialCompleted() { // Rewarded Interstitial View Complete - the interstitial has been played to the end. // You can use this event to initialize your reward Log.d(TAG, "Rewarded interstitial completed!"); // Call method to give reward // giveReward(); } @Override public void onRewardedInterstitialClosed() { // The Rewarded Interstitial ad was closed - this can occur during the interstitial // by closing the app, or closing the end card. Log.d(TAG, "Rewarded interstitial ad closed!"); } }; rewardedInterstitialAd.loadAd( rewardedInterstitialAd.buildLoadAdConfig() .withAdListener(rewardedInterstitialAdListener) .build()); ... } }
private RewardedInterstitialAd rewardedInterstitialAd; @Override public void onCreate(Bundle savedInstanceState) { ... // Instantiate a RewardedInterstitialAd 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). rewardedInterstitialAd = new RewardedInterstitialAd(this, "YOUR_PLACEMENT_ID"); RewardedInterstitialAdListener rewardedInterstitialAdListener = new RewardedInterstitialAdListener() { ... @Override public void onAdLoaded(Ad ad) { // Rewarded interstitial ad is loaded and ready to be displayed Log.d(TAG, "Rewarded interstitial ad is loaded and ready to be displayed!"); rewardedInterstitialAd.show( rewardedInterstitialAd.buildShowAdConfig() .withAppOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) .build()); } ... }; rewardedInterstitialAd.loadAd( rewardedInterstitialAd.buildLoadAdConfig() .withAdListener(rewardedInterstitialAdListener) .build()); ... }
如果廣告在載入後沒有立即顯示,開發人員就需要檢查廣告是否已經無效。成功載入後,廣告的有效期為 60 分鐘。如果您顯示了無效廣告,就無法收取款項。請調用 isAdInvalidated()
以驗證廣告。
Follow the code below, but do not copy the code into your project since this is for example purposes only:
private RewardedInterstitialAd rewardedInterstitialAd; @Override public void onCreate(Bundle savedInstanceState) { ... // Instantiate a RewardedInterstitialAd 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). rewardedInterstitialAd = new RewardedInterstitialAd(this, "YOUR_PLACEMENT_ID"); RewardedInterstitialAdListener rewardedInterstitialAdListener = new RewardedInterstitialAdListener() { ... }; rewardedInterstitialAd.loadAd( rewardedInterstitialAd.buildLoadAdConfig() .withAdListener(rewardedInterstitialAdListener) .build()); // Here is just an example for displaying the ad with delay // Please call this method at appropriate timing in your project showAdWithDelay(); } 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 rewardedInterstitialAd has been loaded successfully if (rewardedInterstitialAd == null || !rewardedInterstitialAd.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 (rewardedInterstitialAd.isAdInvalidated()) { return; } rewardedInterstitialAd.show( rewardedInterstitialAd.buildShowAdConfig() .withAppOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) .build() ); } }, 1000 * 60 * 15); // Show the ad after 15 minutes }
如果您正在使用預設的 Google Android 模擬器,則需要在載入測試廣告前加入以下程式碼:AdSettings.addTestDevice("HASHED ID");
。
首次執行要求以在裝置中載入廣告時,請使用列在 LogCat 的雜湊編號。
若是 Genymotion 與實體裝置,則可跳過這個步驟。如果您想測試實際廣告,請參閱我們的測試指南。
Finally, clean up the object with its destroy
method in your activity's onDestroy
method. Note that you should also use the destroy
method to clean up old ad objects before assigning it to a new instance to avoid memory leak.
@Override protected void onDestroy() { if (rewardedInterstitialAd != null) { rewardedInterstitialAd.destroy(); rewardedInterstitialAd = null; } super.onDestroy(); }
Audience Network 中的影片廣告需要硬件加速顯示才能啟動,否則您的影片畫面可能會出現黑屏。這適用於
如果您的 Target 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" .../>
To make sure your app is notified whether the reward was validated or not, implement the S2SRewardedInterstitialAdListener
interface.
This includes all of the events noted above in the RewardedInterstitialAdListener
interface, as well as two additional events.
After initializing the rewarded video object, you will need to pass in a User ID and Reward amount into the rewarded ad data before loading an ad. Both User ID and Reward amount are strings. For example:
public void loadRewardedInterstitial() { // Instantiate a RewardedInterstitialAd object. rewardedInterstitialAd = new RewardedInterstitialAd(this, "YOUR_PLACEMENT_ID"); // Implement S2SRewardedInterstitialAdListener instead of RewardedInterstitialAdListener RewardedInterstitialAdListener rewardedInterstitialAdListener = new S2SRewardedInterstitialAdListener() { @Override public void onRewardServerFailed() { // Rewarded interstitial ad reward not validated or no response from server Log.d(TAG, "Rewarded interstitial ad reward validation failed"); } @Override public void onRewardServerSuccess() { // Rewarded interstitial ad reward validated server side Log.d(TAG, "Rewarded interstitial ad reward validated!"); } // Other methods from RewardedInterstitialAdListener ... }; // Instantiate RewardData object and include in load config RewardData rewardData = new RewardData("YOUR_USER_ID", "YOUR_REWARD"); rewardedInterstitialAd.loadAd( rewardedInterstitialAd.buildLoadAdConfig() .withRewardData(rewardData) .withAdListener(rewardedInterstitialAdListener) .build()); }
Please note - the server validation callbacks might occur after the end card has been dismissed by a user. Do not deallocate the rewarded interstitial object until after one of these callbacks.
前往 Github 探索我們的 Audience Network Android 程式碼範例。將多個專案匯入到您的整合開發環境 (IDE),然後在裝置或模擬器上運行。
一旦您準備好正式推出應用程式並開始創造收入,請先確定應用程式完全符合 Audience Network 政策以及 Facebook 社群守則後,然後就可以提交您的應用程式以供審查。