Android Studio - How to put AdMob Interstitial

Post on 07-Jan-2017

125 views 0 download

Transcript of Android Studio - How to put AdMob Interstitial

<string name=“interstitial_ad_unit_id">Your AdMob Interstitial ID here</string>

Include required permissions for Google Mobile Ads to run.

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

compile 'com.google.android.gms:play-services-ads:9.6.1'

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

private InterstitialAd mInterstitialAd;

public void loadInterstitial() {

AdRequest adRequest = new AdRequest.Builder() .setRequestAgent("android_studio:ad_template").build(); mInterstitialAd.loadAd(adRequest);}

private void showInterstitial() {

// Show the ad if it's ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { //put code here to execute if ad did not show, example go to next level of a game }}

private InterstitialAd newInterstitialAd() { InterstitialAd interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); interstitialAd.setAdListener(new AdListener() {

@Override public void onAdClosed() { // put code to execute on ad closure, example, go to next level

in a game } }); return interstitialAd;}

mInterstitialAd = newInterstitialAd();loadInterstitial();