Something along these lines. I simply hand-edited it from my implementation for brevity, so there may be syntax errors or something.
But anyway, modify it to suit your needs.
// Setup your default error handler
static Thread.UncaughtExceptionHandler defaultExceptionHandler = null;
Context context = your context
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
defaultExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
for (int nest=0;nest<10 && ex!=null;nest++, ex=ex.getCause()) {
StackTraceElement[] stelist = ex.getStackTrace();
for (StackTraceElement ste : stelist) {
if (ste.getClassName().contains("com.google.android.gms.ads")) {
// Save the last time google play ads crashed.
// Next time our app starts, we won't activate ads if it happened
// within the last N days.
SharedPreferences prefs = Settings.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("admob_crash_time", System.currentTimeMillis());
editor.commit();
break;
}
}
}
if (defaultHandler != null) {
defaultHandler.uncaughtException(thread, ex);
}
}
};
Thread.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);
// Then in your activity startup where you initialize your ads
Context context = your activity or context;
ViewGroup myadframelayout = your frame or parent layout holding the ad;
SharedPreferences prefs = Settings.getPreferences(context);
long lastAdmobCrashTime = prefs.getLong(admob_crash_time, 0);
long timeSinceLastCrash = System.currentTimeMillis() - lastAdmobCrashTime;
// Last crash >= 5 days ago. Try Admob...
if (timeSinceLastCrash >= 5*24*60*60*1000) {
try {
adView = new AdView(context);
adView.setAdUnitId("YOUR ADMOB AD ID");
adView.setAdSize(AdSize.SMART_BANNER);
adView.loadAd(new AdRequest.Builder().build());
myadframelayout.addView(adView);
} catch (Throwable t) {
// No revenue from this guy for today... :(
}
} else {
// Don't run admob. Do whatever you like here.
adView = null;
}
// And in your onResume, onPause, whatnot, if adView is null then don't do anything.