Interstitial ads if use wisely can double your ads income.
This is tutorial from http://tutorialrich.com/extra-how-to-add-admob-interstitial/
Let’s start with the google example source code.Create a new class call SplashScreen.java and add into your project the following code.

// Copyright 2014 Google Inc. All Rights Reserved.
package gnu.airhonkamplify.android;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
/**
* Displays a splash screen image while and loads an interstitial before starting the application.
*/
public class SplashScreenActivity extends Activity {
// How long in milliseconds to wait for the interstitial to load.
privatestaticfinalintWAIT_TIME = 5000;
// Your interstitial ad unit ID.
privatestaticfinal String AD_UNIT_ID = “a15034d40fb4a40″;
private InterstitialAd interstitial;
private Timer waitTimer;
privatebooleaninterstitialCanceled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.splash);
setContentView(image);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// If the interstitial was canceled due to a timeout or an app being sent to the background,
// don’t show the interstitial.
if (!interstitialCanceled) {
waitTimer.cancel();
interstitial.show();
}
}
@Override
public void onAdFailedToLoad(int errorCode) {
// The interstitial failed to load. Start the application.
startMainActivity();
}
});
interstitial.loadAd(new AdRequest.Builder().build());
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
@Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// The interstitial didn’t load in a reasonable amount of time. Stop waiting for the
// interstitial, and start the application.
startMainActivity();
}
});
}
}, WAIT_TIME);
}
@Override
public void onPause() {
// Flip the interstitialCanceled flag so that when the user comes back they aren’t stuck inside
// the splash screen activity.
waitTimer.cancel();
interstitialCanceled = true;
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (interstitial.isLoaded()) {
// The interstitial finished loading while the app was in the background. It’s up to you what
// the behavior should be when they return. In this example, we show the interstitial since
// it’s ready.
interstitial.show();
} elseif (interstitialCanceled) {
// There are two ways the user could get here:
//
// 1. After dismissing the interstitial
// 2. Pressing home and returning after the interstitial finished loading.
//
// In either case, it’s awkward to leave them in the splash screen activity, so just start the
// application.
startMainActivity();
}
}
/**
* Starts the application’s {@link MainActivity}.
*/
private void startMainActivity() {
Intent intent = new Intent(this, AdMobActivity.class);
startActivity(intent);
finish();
}
}
Step 2: Add the drawable folder and the splash.png images.

Step 3. Add the .SplashScreenActivity in the AndroidManifest.xml and intent-filter as start up.
<activity
android:name=“.SplashScreenActivity”
android:label=“@string/app_name”>
<intent-filter>
<actionandroid:name=“android.intent.action.MAIN”/>
<categoryandroid:name=“android.intent.category.LAUNCHER”/>
</intent-filter>
</activity>

Step 4: Run the code.

How to add admob interstitial
Reviewed by Admin
on
5:54 PM
Rating:
No comments: