Sunday, February 23, 2014

Integrating Google ads for mobile into an android app

makesure you have ant debug/release runs well with your application root folder with google-play-services-lib and you have creted the adUnitID for Banners/InterstitialAd with an account for your android app to monitize (to show google ads in your app and make money)

Now in your application main file the class which is the etry point of your Activity or which extends CordovaACtivity you should (not necessarily true always) implement the Banners/InterstitialAd.

How to implement Banners


import the following packages

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

declare variables to hold the objects

private static final String AdMob_banner_Ad_Unit = "GOOGLE_ADMOB_BANNER_ID";
private AdView m_adView; 

in  public void onCreate(Bundle savedInstanceState) function 
write the below code

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
                
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
        
        m_adView = new AdView(this);
        m_adView.setAdUnitId(AdMob_banner_Ad_Unit);
        m_adView.setAdSize(AdSize.BANNER);
         
        LinearLayout layout = super.root;
        layout.addView(m_adView); 
        AdRequest request1 = new AdRequest.Builder().build();
        m_adView.loadAd(request1);
}

also update the other override methods as

    @Override
    public void onPause() {
    m_adView.pause();
      super.onPause();
    }

    @Override
    public void onResume() {
      super.onResume();
      m_adView.resume();
    }

    @Override
    public void onDestroy() {
    m_adView.destroy();
      super.onDestroy();
    }


To Implement InterstitialAd

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

declare variables to hold the objects

private InterstitialAd m_interstitial;
private static final String AdMob_interstitial_Ad_Unit = "GOOGLE_ADDMOB_INTERSTITIAL_UNIT_ID";

in  public void onCreate(Bundle savedInstanceState) function 
write the below code

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());
        
        // Create the interstitial.
        m_interstitial = new InterstitialAd(this);
        m_interstitial.setAdUnitId(AdMob_interstitial_Ad_Unit);
        
        m_interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                //super.onAdLoaded();
                displayInterstitial();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                //super.onAdFailedToLoad(errorCode);
            }
        });

     // Create ad request.
        AdRequest adRequest2 = new AdRequest.Builder()
        .build();

        // Begin loading your interstitial.
        m_interstitial.loadAd(adRequest2);         
}

 // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
      if (m_interstitial.isLoaded()) {
     m_interstitial.show();
      }
    }

No comments:

Post a Comment