使用 Audience Network,您便可通过 Facebook 广告让 Android 应用实现变现。插屏广告是可以在应用中显示的一种全屏广告,通常在应用页面切换时显示。例如,通过游戏关卡或者加载完新闻应用中的报道后。
请确保先阅读 Audience Network 入门指南和 Android 入门指南,然后再继续操作。
Android Audience Network SDK 5.1 版中已添加此方法。
5.3.0
及以上版本的 Audience Network 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); } ... }
在 Activity 类的顶部添加以下代码,以便导入 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 } }
最后,在 Activity 类的 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 清单文件中,向 <application>
标签添加下列属性,为整个应用程序启用硬件加速功能:
<application android:hardwareAccelerated="true" ...>
如果您只想为应用程序中的特定活动启用此功能,您可以向 Android 清单文件中的 <activity>
标签添加下列功能。以下示例将为用于显示插屏广告和奖励式视频广告的 AudienceNetworkActivity
启用硬件加速功能:
<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>
前往 Github 查看我们的 Audience Network Android 代码示例。将项目导入您的 IDE,然后在设备或模拟器上运行。
准备好让您的应用加入 Audience Network 并开始盈利后,请先确保应用符合 Audience Network 政策和 Facebook 社区守则,然后提交应用供审核。