Thursday 3 October 2013

How to implement In-Mobi ads in android?


Step 1 :- Signup in-mobi as a ad-network.   http://www.inmobi.com/

Step 2:- Select my properties and add new property.

Step 3 :- Get Property ID : 4bf5be5909154b828ea4a6f87757f49e

Step 4 :- Add Permission in manifest file.

Download jar file :- InMobiAdNetwork-3.7.1.jar and InMobiCommons-3.7.1.jar

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name ="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
   
   
    <uses-permission android:name ="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name= "android.permission.GET_TASKS" />

Add activity in your application tag

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

Step 5 :- Add this code in main.xml file

  <com.inmobi.androidsdk.IMAdView android:layout_width="320dp"
android:layout_height="50dp" android:id="@+id/imAdview" appId="4bf5be5909154b828ea4a6f87757f49e"
adSize="15" />

Step 6 :- Add this in main.java file.

package com.example.inmobi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.InflateException;
import android.view.Menu;
import android.widget.Toast;

import com.inmobi.androidsdk.IMAdInterstitial;
import com.inmobi.androidsdk.IMAdInterstitialListener;
import com.inmobi.androidsdk.IMAdRequest;
import com.inmobi.androidsdk.IMAdRequest.ErrorCode;
import com.inmobi.androidsdk.IMAdView;

public class MainActivity extends Activity {
    IMAdView imAdView;
    private IMAdInterstitial mIMAdInterstitial;
  private static String LOG_TAG="InMobiAndroidSampleApp";
private IMAdRequest mAdRequest;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
         imAdView = (IMAdView) findViewById(R.id.imAdview);
       
         try {   
               
               
                mAdRequest = new IMAdRequest();
                imAdView.setIMAdRequest(mAdRequest);

                //load new ad has to be called explicitly..Just adding xml to layout wont call load new ad
                imAdView.loadNewAd(mAdRequest);
               
                // initialize an interstitial ad
                mIMAdInterstitial = new IMAdInterstitial(this,
                        "4bf5be5909154b828ea4a6f87757f49e");
                mIMAdInterstitial.loadNewAd(mAdRequest);
                // set the listener if the app has to know ad status notifications
                mIMAdInterstitial.setIMAdInterstitialListener(mIMAdInListener);   

            }catch(InflateException e){
               
                e.printStackTrace();
            }catch(IllegalArgumentException e){
               
                e.printStackTrace();
            }catch(NullPointerException e){
               
                e.printStackTrace();
            }
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private IMAdInterstitialListener mIMAdInListener = new IMAdInterstitialListener() {

        @Override
        public void onShowAdScreen(IMAdInterstitial adInterstitial) {
            Log.i(LOG_TAG,
                    "InMobiAdActivity-> onShowAdScreen, adInterstitial: "
                            + adInterstitial);
        }

        @Override
        public void onDismissAdScreen(IMAdInterstitial adInterstitial) {
            Log.i(LOG_TAG,
                    "InMobiAdActivity-> onDismissAdScreen, adInterstitial: "
                            + adInterstitial);

        }

        @Override
        public void onAdRequestFailed(IMAdInterstitial adInterstitial,
                ErrorCode errorCode) {
            Log.i(LOG_TAG,
                    "InMobiAdActivity-> onAdRequestFailed, adInterstitial: "
                            + adInterstitial + " ,errorCode: " + errorCode);
            Toast.makeText(MainActivity.this,
                    "Interstitial Ad failed to load. Errorcode: " + errorCode,
                    Toast.LENGTH_SHORT).show();
       
        }

        @Override
        public void onAdRequestLoaded(IMAdInterstitial adInterstitial) {
            Log.i(LOG_TAG,
                    "InMobiAdActivity-> onAdRequestLoaded, adInterstitial: "
                            + adInterstitial);
            Toast.makeText(MainActivity.this, "Interstitial Ad Loaded.",
                    Toast.LENGTH_SHORT).show();
           
            mIMAdInterstitial.show();
           
        }

        @Override
        public void onLeaveApplication(IMAdInterstitial adInterstitial) {
            Log.i(LOG_TAG,
                    "InMobiAdActivity-> onLeaveApplication, adInterstitial: "
                            + adInterstitial);
        }
    };
   
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
       
        if(imAdView!=null)
                imAdView.destroy();
           
            if(mIMAdInterstitial!=null)
                mIMAdInterstitial.destroy();
    }
}

Step 7 :- Run project.

Out/put :-




Download Demo Project : -  https://dl.dropboxusercontent.com/u/109954727/InMobi.zip








4 comments:

  1. hi - thank you for your example. may i have a copy of your project? the link does not work. thank you

    ReplyDelete
  2. hiiiiiiiiiiiiii where is demo

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete