MoPub InMobi Android

From InMobi Wiki
Jump to: navigation, search

Contents

Integrating MoPub with InMobi Android SDK

This document describes the steps to integrate MoPub SDK v1.10.0.5 with the InMobi Android SDK v3.7

Getting Started

Before you start integrating MoPub with the InMobi Android SDK, you need to familiarize yourself with the instructions provided in the following links:

Specifying a Custom Event

To specify a custom event using the MoPub web user interface, perform the following steps:

  1. Login to MoPub’s web UI.
  2. From the Network page, click Add a Network.
  3. Under Add a Network, select Custom Native Network.
  4. In Custom Native Network, add the Custom Class name (for example, com.company.project.InMobiBanner for banner ads). Place the fully qualified class name of your custom event (for example, com.company.project.YourCustomEventBanner) in the Custom Class column.
  5. Add another Custom Class (for example, com.company.project.InMobiInterstitial for interstitial ads).
  6. Enable the Custom Class for your app by selecting the required check box.

The MoPub SDK will be able to automatically instantiate your CustomEventBanner sub-class when your application is running.

Screenshots

Setting Custom Class

Test App Name

Mopubandroid1.png

Setting Fully Qualified Banner Class Name

Mopubandroid2.png

Setting Fully Qualified Interstitial Class Name

Mopubandroid3.png

Enabling Apps

Mopubandroid4.png

Using Custom Events to Support the InMobi Network

The code shown in this section demonstrates how to implement InMobi native ads using custom events.

Include the InMobi SDK in your project.

1. Using the web interface, register a custom class name (say com.mopub.custom.InMobiBanner) for banner ad units. Refer to the Specifying a Custom Event section for more information. 2. Create a MoPubView (for example, mMopubBannerView) in your activity and set the AdUnitId to the ID provided during creation of AdUnit. Refer to the MoPub Banner Ads documentation for more information. 3. Implement the custom class InMobiBanner (under package com.mopub.custom) in your Activity as shown in the sample code below. 4. Add the listeners and the callback functions for ad load success and failure. 5. Introduced HTML5 video capability in Android. To support this, hardware acceleration must be enabled. Publishers are recommended to add the property android:hardwareAccelerated="true" in the application tag (alternatively, in all the activities that display InMobi ads).

Example:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
         > 

In your app, if you observe the ad flickering (known issue in Android), you can disable hardware acceleration on the adview and the banner objects, by using the disableHardwareAcceleration API.

Example:

mInMobiBanner.disableHardwareAcceleration();

However, we recommend not to disable hardware acceleration as bad user experience may be caused if HTML5 video is played.

6. The TestMode method is deprecated. To get test ads, you need to follow the instructions mentioned in the following URL: http://www.inmobi.com/helpcenter/publisher-help/integration/how-to-set-up-publisher-diagnostics/

Sample Code

Manifest Changes

Add the following activity in your manifest file:

<activity
     android:name="com.inmobi.androidsdk.IMBrowserActivity"
     android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize" 
     android:hardwareAccelerated="true”
/>
Layout Changes
// to get the view of Banner ad
<com.mopub.mobileads.MoPubView
   android:id="@+id/bannerview"
   android:background="@drawable/spacer"
   android:layout_width="fill_parent"
   android:layout_height="50dp"
   android:layout_marginTop="10dp" />
Code Changes

InMobiMopubCustomEventActivity.java

// onCreate Method to fetch Banners Ads
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   BtnRefreshAd = (Button)findViewById(R.id.btnRefreshAd);
   BtnLoadInterstitial = (Button)findViewById(R.id.btnLoadInterstitial);
   BtnRefreshAd.setOnClickListener(this);
   BtnLoadInterstitial.setOnClickListener(this);

   // Initialize Ad components
   mMopubBannerView = (MoPubView) findViewById(R.id.bannerview);
   mMopubBannerView.setAdUnitId("YOUR_MOPUB_BANNER_ADUNIT_ID");

   // loadAd will call InMobiBanner::loadAd 
   mMopubBannerView.loadAd(); 
   mMopubBannerView.setAutorefreshEnabled(false);  
}
Custom Class

InMobiBanner.java

// Method to load banner ads using InMobi SDK

    public void loadAd(Context context, CustomEventBanner.Listener bannerListener,
            Map<String, Object> localExtras, Map<String, String> serverExtras) {
        mBannerListener = bannerListener;
        
        Activity activity = null;
        if (context instanceof Activity) {
            activity = (Activity) context;
        } else {
            // You may also pass in an Activity Context in the localExtras map and retrieve it here.
        }
        
        if (activity == null) {
            mBannerListener.onAdFailed();
            return;
        }
        
        /*
         * You may also pass this String down in the serverExtras Map by 
         * specifying Custom Event Data in MoPub's web interface.
         */
        String inMobiAppId = "YOUR_APP_ID";
        mInMobiBanner = new IMAdView(activity, IMAdView.INMOBI_AD_UNIT_320X50, inMobiAppId);
        IMAdRequest adRequest = new IMAdRequest();
        
        //adRequest.setTestMode(true);
        //adRequest.setAreaCode("areacode");
        //adRequest.setEducation(EducationType.Edu_BachelorsDegree);
        //adRequest.setGender(GenderType.MALE);
        //adRequest.setIncome(1000);
        //adRequest.setAge(23);
    	
        Map<String, String> map = new HashMap<String, String>();
        map.put("tp", "c_mopub");
        adRequest.setRequestParams(map);

        mInMobiBanner.setIMAdListener(this);
        mInMobiBanner.loadNewAd(adRequest);
        mInMobiBanner.setRefreshInterval(IMAdView.REFRESH_INTERVAL_OFF);
    }

      // Refer to the Sample App to know more about Callback functions 
      // for Banner Ads to be implemented by mIMAdListener

}

Interstitial Ads

1. Using the web interface, register a custom method name (for example, inmobiInterstitial) for interstitial ads. Refer to the Specifying a Custom Event section for more information.

2. Create a MoPubInterstitial (for example, mMopubInterstitialView) in your activity and set the AdUnitId to the ID provided during the creation of AdUnit. Refer to the MoPub Interstitial Ads documentation for more information.

3. Implement the custom class inmobiInterstitial (under package com.mopub.custom) in your activity as shown in the sample code below.

4. Add the listeners and the callback functions for ad load success and failure.

5. Introduced HTML5 video capability in Android. To support this, hardware acceleration must be enabled. Publishers are recommended to add the property android:hardwareAccelerated="true" in the application tag (alternatively, in all the activities that display InMobi ads).

Example:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
         > 

In your app, if you observe the ad flickering (known issue in Android), you can disable hardware acceleration on the adview and the interstitial objects, by using the disableHardwareAcceleration API.

Example:

mInMobiInterstitial.disableHardwareAcceleration();

However, we recommend not to disable hardware acceleration as bad user experience may be caused if HTML5 video is played.

6. The TestMode method is deprecated. To get test ads, you need to follow the instructions mentioned in the following URL: http://www.inmobi.com/helpcenter/publisher-help/integration/how-to-set-up-publisher-diagnostics/

Sample Code

Manifest Changes

Add the following activity in your manifest file:

<activity
    android:name="com.inmobi.androidsdk.IMBrowserActivity"
    android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize" 
android:hardwareAccelerated="true"
/>
Code Changes

InMobiMopubCustomEventActivity.java

// method to call Interstitial ads
public void getInterstitialAd() {
   mMopubInterstitialView = new MoPubInterstitial(this, "YOUR_MOPUB_INTERSTITIAL_ADUNIT_ID");
   //load method will call InMobiInterstitial::loadInterstitial method
   mMopubInterstitialView.load();

}

// method to show interstitial ads	
public void showInterstitial() {
   mMopubInterstitialView.setListener(this);  // Register your activity as a listener prior to loading the ad
   mMopubInterstitialView.load();
   }
}
Custom Class

InMobiInterstitial.java

// method to load interstitial ads using InMobi Sdk
public void loadInterstitial(Context context, CustomEventInterstitial.Listener interstitialListener,
        Map<String, Object> localExtras, Map<String, String> serverExtras) {
        mInterstitialListener = interstitialListener;
        Activity activity = null;
        if (context instanceof Activity) {
            activity = (Activity) context;
        } else {
            // You may also pass in an Activity Context in the localExtras map and retrieve it here.
        }
        
        if (activity == null) {
            mInterstitialListener.onAdFailed();
            return;
        }
        
        /*
         * You may also pass this String down in the serverExtras Map 
         * by specifying Custom Event Data in MoPub's web interface.
         */
        String inMobiAppId = "YOUR_APP_ID";
        mInMobiInterstitial = new IMAdInterstitial(activity, inMobiAppId);
        IMAdRequest adRequest = new IMAdRequest();
        // Examples to pass Demographic Data 
        //adRequest.setTestMode(true);
        //adRequest.setAreaCode("areacode");
        //adRequest.setEducation(EducationType.Edu_BachelorsDegree);
        //adRequest.setGender(GenderType.MALE);
        //adRequest.setIncome(1000);
        //adRequest.setAge(23);
        Map<String, String> map = new HashMap<String, String>();
        map.put("tp", "c_mopub");
        adRequest.setRequestParams(map);
        mInMobiInterstitial.setIMAdInterstitialListener(this);
        mInMobiInterstitial.loadNewAd(adRequest);
    }

// Method to show interstitial ads 
    @Override
    public void showInterstitial() {
        mInMobiInterstitial.show();
    }

//Refer Sample App to know more about Callback functions for Interstitial Ads

Supported Ad Formats

Both banner ads as well as interstitial ads are supported. The following banner ad units are supported:

More Information

For more details on implementation, you can refer to the sample app provided.

For further queries, refer to the MoPub Wiki link: https://github.com/mopub/mopub-client/wiki/CustomEventsAndroid

For any integration queries, please Contact Us.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox