Btricks Official
Btricks Official

Reputation: 13

Facebook Audience Network and Admob at same activity

I want to use both adnetwork at same time mean if admob ads not loaded /show then i want to show facebook ads.

can i use facebook banner & admob interstitial at same screen (is this safe ?)

Upvotes: 1

Views: 656

Answers (1)

anime kings
anime kings

Reputation: 26

public class SomeActivity extends AppCompatActivity {

private InterstitialAd mInterstitialAd;
private com.facebook.ads.InterstitialAd fbIntAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page);
    initAdMobInt();
    initFBIntAd();
    TextView showAds =  findViewById(R.id.show_ads);
    showAds.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (fbIntAd.isAdLoaded()
                    && !fbIntAd.isAdInvalidated()) {
                fbIntAd.show();
            } else if (mInterstitialAd.isLoaded()) {
                adMobIntAd.show();
            }   else {
                // perform required action
            }

        }
    });
}

private void initAdMobInt() {
    adMobIntAd = new InterstitialAd(this);
    adMobIntAd.setAdUnitId(getString(R.string.admob_int_ad));
    adMobIntAd.loadAd(new AdRequest.Builder().build());

    adMobIntAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            // Load the next interstitial.
            adMobIntAd.loadAd(new AdRequest.Builder().build());
            // Perform the action here
        }

        @Override
        public void onAdFailedToLoad(int i) {
            adMobIntAd.loadAd(new AdRequest.Builder().build());
        }

        @Override
        public void onAdLeftApplication() {
            adMobIntAd.loadAd(new AdRequest.Builder().build());
        }
    });
}

private void initFBIntAd() {
    fbIntAd = new com.facebook.ads.InterstitialAd(this, getString(R.string.fb_int_ad));
    // Set listeners for the Interstitial Ad
    fbIntAd.setAdListener(new InterstitialAdListener() {
        @Override
        public void onInterstitialDisplayed(Ad ad) {
        }

        @Override
        public void onInterstitialDismissed(Ad ad) {
            //Perform action here .....
            fbIntAd.loadAd();
        }

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

        @Override
        public void onAdLoaded(Ad ad) {
        }

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

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

    fbIntAd.loadAd();
}

}

Upvotes: 1

Related Questions