Audience bidding for Custom Mediation (Android)

The Audience Bidding Android SDK integration guide (available here) will help you with the following:

  • Adding the InMobi SDK to your Project
  • Adding and Verifying the Dependencies
  • Changing the Manifest file
  • Preparing your App for Android N+
  • Initializing the InMobi SDK

Once you have added the SDK successfully, please follow the steps below to load the following ad units using Audience Bidding.

  1. Banner Ads
  2. Interstitial Ads

Banner Ads

Step 1: Setting up a Banner Ad

To display a banner ad on your app, you would first need to create a banner placement ID. After adding your app, select BANNER AD to create a placement for the ad type - Banner. Once you create the banner placement, the placement ID is available.

Step 2: Adding a Banner Ad to your App

Creating a Banner Ad

To add a banner ad in Android code, create an instance of InMobiBanner:

    InMobiBanner bannerAd = new InMobiBanner(BannerAdsActivity.this, );
    

Notes:

  • The InMobiBanner class is not thread-safe. A banner instance must be created on the UI thread.
  • Similarly, all methods on this instance must be called on the UI thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.
  • Creating an InMobiBanner object before SDK initialization will throw an exception
Set Banner Ad size

To set the banner size in Android code, write the following code:

    bannerAd.setBannerSize(, );
    

Step 3: Requesting for Bid

Requesting for a bid requires you to set the size of the Banner ad before requesting for a bid on the InMobiBanner instance. Once you have set the size of InMobiBanner ad, you can call the InMobiBanner#getPreloadManager()#preload() method on the instance you just created to request for a bid.

    /**
    * A sample code to request bid
    */
    public class YourActivity {
    private InMobiBanner banner;
        public void requestBid() {
        banner.setListener(new BannerAdEventListener() {
                /**
                * Called to notify that an ad was successfully loaded.
                * @param ad Represents the {@link InMobiBanner} ad which was loaded
                * @param info Represents the ad meta information
            */
                @Override
                public void onAdFetchSuccessful(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {
                    double bid = info.getBid();
                    Log.d(TAG, "Bid received : " + bid);
                }
            /**
                * Called to notify that an ad preload has failed. 

Note This * notification is given only when you use {@code preload()} in * {@link InMobiBanner#getPreloadManager()}

* * @param ad Represents the ad which was preloaded * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason */ @Override public void onAdFetchFailed(@NonNull InMobiBanner ad, @NonNull InMobiAdRequestStatus status) { Log.d(TAG, "Bid fetch failure : " + status.getMessage()); } }); // Step to preload banner banner.getPreloadManager().preload(); } }

Note:

You’ve to maintain this banner instance for loading the ad later, in case InMobi won the auction.

Step 4: Loading the Banner Ad

In case InMobi has won the auction, then you can call the InMobiBanner#getPreloadManager()#load() method on the instance to load the ad.

    /**
    * A sample code to load ad
    */
    public class InMobiCustomEvent {
        private InMobiBanner banner;
        /**
        * A sample load method for your PPM
        */
        @Override
        protected void loadBanner() {
            // Custom event load, publisher to hold InMobiBanner object
            this.banner.setListener(new BannerAdEventListener() {
            /**
                * Called to notify that an ad was successfully loaded and is ready to be displayed.
                *
                * @param ad   Represents the ad which was loaded
                * @param info Represents the ad meta information
            */
                @Override
                public void onAdLoadSucceeded(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {
                    Log.d(TAG, "Banner displayed");
                }

                /**
                    * Called to notify that a request to load an ad failed.
                    *
                    * @param ad     Represents the ad which failed to load
                    * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason
                */
                @Override
                public void onAdLoadFailed(@NonNull InMobiBanner ad, InMobiAdRequestStatus status) {
                    Log.d(TAG, "Banner display failed");
                }
            });
            this.banner.getPreloadManager().load();
        }

        // Other code
    }
    

Interstitial Ads

Step 1: Setting up an Interstitial Ad

  1. To display an Interstitial ad, you need an interstitial placement ID.
  2. After adding your app, select INTERSTITIAL AD to create a placement for ad type Interstitial.
  3. Once you successfully create the placement, the placement ID is available.

Step 2: Adding an Interstitial Ad to your App

Creating an Interstitial Ad

To create an interstitial ad, create an instance of an InMobiInterstitial like so:

InMobiInterstitial interstitialAd = new InMobiInterstitial(InterstitialAdsActivity.this, , 
mInterstitialAdEventListener);

Notes:

  • The InMobiInterstitial class is not thread-safe. An interstitial instance must be created on the UI thread.
  • Similarly, all methods on this instance must be called on the UI thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.
  • Creating an InMobiInterstitial object without SDK Initialization will throw an exception

Step 3: Requesting for Bid

Requesting for a bid requires you to call the API InMobiInterstitial#getPreloadManager()#preload() method on the instance you just created to request for a bid.

/**
* A sample code to request bid
*/
public class YourActivity {
    private InMobiInterstitial interstitial;

   public void requestBid() {
       interstitial = new InMobiInterstitial(context, , new InterstitialAdEventListener() {
                    /**
                    * Called to notify that an ad was successfully loaded.
                    * @param ad Represents the {@link InMobiInterstitial} ad which was loaded
                    * @param info Represents the ad meta information
                    */
                    @Override
                    public void onAdFetchSuccessful(@NonNull InMobiInterstitial ad, @NonNull AdMetaInfo info) {
                        double bid = info.getBid();
                        Log.d(TAG, "Bid received : " + bid);
                    }
                    /**
                    * Called to notify that an ad preload has failed. 

Note This * notification is given only when you use {@code preload()} in * {@link InMobiInterstitial#getPreloadManager()}

* * @param ad Represents the ad which was preloaded * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason */ @Override public void onAdFetchFailed(@NonNull InMobiInterstitial ad, @NonNull InMobiAdRequestStatus status) { Log.d(TAG, "Bid fetch failure : " + status.getMessage()); } }); // Step to preload interstitial interstitial.getPreloadManager().preload(); } }

Note:

You’ve to maintain this interstitial instance for loading the ad later, in case InMobi won the auction.

Step 4: Loading & Showing the Interstitial Ad

In case InMobi has won the auction, then you can call the InMobiInterstitial#getPreloadManager()#load() method on the instance to load the ad followed by InMobiInterstitial#show() to show the ad.

/**
* A sample code to load ad
*/
public class InMobiCustomEvent {
    private InMobiInterstitial interstitial;
    /**
    * A sample load method for your PPM
    */
    @Override
    protected void loadInterstitial() {
            // Custom event load, publisher to hold InMobiInterstitial object
            interstitial.setListener(new InterstitialAdEventListener() {
                /**
                * Called to notify that an ad was successfully loaded and is ready to be displayed.
                *
                * @param ad   Represents the ad which was loaded
                * @param info Represents the ad meta information
               */
                @Override
                public void onAdLoadSucceeded(@NonNull InMobiInterstitial ad, @NonNull AdMetaInfo info) {
                     Log.d(TAG, "Interstitial loaded");
                }

               /**
                * Called to notify that a request to load an ad failed.
                *
                * @param ad     Represents the ad which failed to load
                * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason
               */
                @Override
                public void onAdLoadFailed(@NonNull InMobiInterstitial ad, @NonNull InMobiAdRequestStatus status) {
                    Log.d(TAG, "Interstitial load failed");
                }
        });
        this.interstitial.getPreloadManager().load();
   }
    /**
    * A sample show method for your PPM
    */
    @Override
    protected void showInterstitial() {
        if (this.interstitial.isReady()) {
            this.interstitial.show();
        }
    }
    // Other code
}