Android SDK Usage

Important: When building and testing your apps, make sure you use test ads rather than production ads. Failure to do so can lead to suspension of your account. Please see the Optional Parameters section for more info on toggling the test ads.

Banner Ads

Once the initialization of the SDK is complete, you can get banner ads from the EMAManager instance by using the following method:

ArrayList<DFPBannerManager> bannerManagers = EMAManager.getInstance().getBannerManagers(zoneIds)

Now that you have a bannerManagers array, you can set a manager's AdStatusListener. An AdStatusListener is used for getting callbacks during the ads' lifecycles. Depending on the callbacks, you may either display the ad to the user or skip it entirely.

bannerManager.setListener(this) // AdStatusListener

Finally, you can load your ad:

bannerManager.loadBannerAd()

Once the bannerManager.loadBannerAd method is called, one of these three will happen:

  • bannerStatusChanged will be called with status of bannerManager set to AdStatus.READY.
  • bannerStatusChanged will be called with status of bannerManager set to AdStatus.FAILED.
  • If ads are disabled or the user in an unfinished ad session, then bannerManager.loadBannerAd call will be ignored and nothing will happen.

As AdStatusListener, you should listen to the bannerStatusChanged events and display the ad based on its status.

override fun DFPBannerStatusChanged(manager: DFPBannerManager) {
if (manager.status == AdStatus.READY) {
mView.addView(manager.banner)
manager.adIsVisible() // Enable auto refresh
}
}

Note: In the full example code you will see sample zoneIds 123456 and 123564. You will be given your actual zoneIds by us, and you will have to replace these sample ids with actual zone ids.

Full Example

public class MyActivity extends AppCompatActivity implements AdStatusListener {
private RelativeLayout relativeLayoutFor123456;
private RelativeLayout relativeLayoutFor123465;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
relativeLayoutFor123456 = findViewById(R.id.relativeLayoutFor123456);
relativeLayoutFor123465 = findViewById(R.id.relativeLayoutFor123465);
this.loadBanner();
}
private void loadBanner() {
ArrayList<String> zoneIds = new ArrayList<String>();
zoneIds.add("123456");
zoneIds.add("123465");
ArrayList<DFPBannerManager> bannerManagers = EMAManager.getInstance().getBannerManagers(zoneIds);
if (bannerManagers.size() > 0) {
DFPBannerManager bannerManager1 = bannerManagers.get(0); //Returns the banner manager for zone with id 123456
DFPBannerManager bannerManager2 = bannerManagers.get(1); //Returns the banner manager zone with id 123465
bannerManager1.setListener(this); // Ad Status Listener
bannerManager1.loadBannerAd();
bannerManager2.setListener(this);
bannerManager2.loadBannerAd();
}
}
@Override public void DFPBannerStatusChanged(@NotNull DFPBannerManager manager) {
if (manager.getStatus() == AdStatus.READY) {
manager.adIsVisible(); // Set ad status as visible to enable auto refreshing
if(manager.getBannerModel().getAdConstraints().getZoneId().equals("123456")) {
relativeLayoutFor123456.addView(manager.getBanner());
} else if(manager.getBannerModel().getAdConstraints().getZoneId().equals("123465")) {
relativeLayoutFor123465.addView(manager.getBanner());
}
}
}
}

Important Note: When you leave the page ads displayed, you have to call adIsInvisible() method of the related banner manager.

Sticky Banner Ads

Sticky banner ads are usually displayed at the bottom of the screen or above the tab bar. These banner ads are always visible to the user.

In order to display a sticky banner, you have to use RelativeLayout as root view in your layout xml and set current activity before calling displaySticky() method. Then, you need to call the displaySticky method of StickyBannerManager.instance in your Activity's onResume() method.

Full Example

public class MyActivity extends AppCompatActivity implements AdStatusListener {
private ArrayList<String> zoneIds = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
zoneIds.add("123456") // Zone id for sticky ad
}
@Override protected void onResume() {
super.onResume();
StickyBannerManager.getInstance().displaySticky(zoneIds);
}
}

By setting StickyBannerManager's listener you can listen to the status events of sticky banner ads. These events are:

  • AdStatus.READY sticky banner is ready for display.
  • AdStatus.FAILED sticky banner is failed to load an ad.
  • AdStatus.PRESENT sticky banner is recorded as impression.
  • AdStatus.WILL_LEAVE sticky banner is redirecting user to a URL.

If you wish to hide the sticky banner just hideSticky() method of StickyBannerManager class.

Interstitial Ad

Interstitial ads are full-screen ads that cover the interface of their host app.

Full Example

public class MyActivity extends AppCompatActivity implements AdStatusListener {
private ArrayList<String> zoneIds = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
zoneIds.add("123456") // Zone id for interstitial ad
}
@Override protected void onResume() {
super.onResume();
InterstitialDisplayManager.getInstance().displayInterstitial(zoneIds);
}
}

By setting InterstitialDisplayManager's listener you can listen to the status events of interstitial ads. These events are:

  • AdStatus.READY: interstitial is ready for display.
  • AdStatus.FAILED: insterstitial is failed to load an ad.
  • AdStatus.PRESENT: interstitial is displayed and is on screen.
  • AdStatus.WILL_LEAVE: interstitial is redirecting user to a URL.
  • AdStatus.USED: interstitial is dismissed and needs reloading for another display

Rewarded Ads

Rewarded ads are the ads where users have the option of interacting with in exchange for in-app rewards.

Before you load and show a rewarded ad you must set EMASettings.instance.activity = currentActivity and the listener for the status of ad RewardedAdDisplayManager.instance.listener = this.

Full Example

public class MyActivity extends AppCompatActivity implements AdStatusListener {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
RewardedAdDisplayManager.getInstance().loadRewarded()
}
@Override public void rewardedStatusChanged(@NotNull RewardedAdManager manager) {
if(manager.getStatus().equals(AdStatus.READY)) {
RewardedAdDisplayManager.getInstance().showRewarded();
}
}
}

By setting RewardedAdDisplayManager's listener you can listen to the status events of rewarded ads. These events are:

  • AdStatus.READY: rewarded is ready for display.
  • AdStatus.FAILED: rewarded ad is failed to load.
  • AdStatus.OPENED: rewarded ad is started to play and is on screen.
  • AdStatus.REWARDED: user complete playing and earns reward.
  • AdStatus.CLOSED: rewarded ad is closed either by user or complete playing.

IMA Preroll Ads

IMA preroll ads can be displayed on ExoPlayer instances.

In order to be able to display a preroll ad, first you must request ads from the SDK:

// Set video to AVPlayer
...
// After setting the video request ads
IMADisplayManager.getInstance().setListener(this) // AdStatusListener
IMADisplayManager.getInstance().requestAd(["YOUR_AD_CATEOGRY"], playerContainer, exoPlayer)

By setting IMADisplayManager's listener you can listen to the status events of preroll ads. These events are:

  • AdStatus.READY: preroll ad is ready for display.
  • AdStatus.FAILED: preroll ad is either failed to play or failed to load.
  • AdStatus.ACTIVE: preroll ad is being played.
  • AdStatus.PAUSED: preroll ad is paused by the user.