如何在Android应用中正确使用Facebook非页内广告

时间:2018-10-04 10:56:15

标签: java android facebook facebook-monetization-manager

我是Facebook Audience Network Ads的新手。我想在点击按钮时放置插页式广告。下面是我的代码。

我想检查这是否是正确的实施方式(特别是显示一次后重新加载广告)

private void initInterstitialAd() {
        interstitialAd = new InterstitialAd(this, getString(R.string.fb_interstitial_ad));
        // Set listeners for the Interstitial Ad
        interstitialAd.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                doAction(pos);
                interstitialAd.loadAd();
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                interstitialAd.loadAd();
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                // Show the ad
//                if (!interstitialAd.isAdInvalidated())
//                    interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
            }
        });

        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAd.loadAd();
    } 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    initInterstitialAd();
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pos = viewPager.getCurrentItem();
            if (random.nextBoolean() && random.nextBoolean()
                    && interstitialAd.isAdLoaded()
                    && !interstitialAd.isAdInvalidated()) {
                interstitialAd.show();
            } else {
                doAction(pos);
            }

        }
    });
}

0 个答案:

没有答案