/**
* Load Admob banner is as backup for Native Ads
*
* @param adPlaceHolder the view to be populated
*/
private void populateRegularBannerAd(final FrameLayout adPlaceHolder) {
Log.i(TAG, "Loading Banner Ad");
bannerAdView = new AdView(MyApp.getAppContext());
bannerAdView.setAdSize(AdSize.LARGE_BANNER);
bannerAdView.setAdUnitId(MyApp.getAppContext().getResources().getString(R.string.admob_banner_id));
bannerAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
BANNER_ADS_NB_SUCCESSIVE_FAIL = 0;
Log.i(TAG, "Banner Ad loaded OK");
}
@Override
public void onAdFailedToLoad(int errorCode) {
Log.w(TAG, "Failed to load banner ad: " + errorCode);
if (Utils.isOnline())
BANNER_ADS_NB_SUCCESSIVE_FAIL++;
Utils.freeMemory();
populateBackupBannerAd(adPlaceHolder);
}
@Override
public void onAdLeftApplication() {
Intent broadcast = new Intent();
broadcast.setAction("CLOSE_DIALOG");
MyApp.getAppContext().sendBroadcast(broadcast);
}
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(TEST_DEVICE)
.build();
bannerAdView.loadAd(adRequest);
if (adPlaceHolder != null) {
try {
adPlaceHolder.removeAllViews();
adPlaceHolder.addView(bannerAdView);
adPlaceHolder.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
}
@Override
public void onViewDetachedFromWindow(View view) {
// if not destroyed, the banner keeps trying to refresh even when detached
if (bannerAdView != null) {
try {
Log.i(TAG, "destroying Banner............");
bannerAdView.pause();
bannerAdView.setAdListener(null);
adPlaceHolder.removeAllViews();
bannerAdView.destroy();
bannerAdView = null;
} catch (Exception e) {
// do nothing
}
}
}
});
} catch (Exception e) {
// do nothing
e.printStackTrace();
}
}
}