Sup guys, I need some help getting admob interstitals to work, I have the banners working but admob interstitals not working.
Can someone give me working code to have it display immediately, since this will be in live wallpapers.
I need the imports as well. I want it to display as soon as the app is launched. Also is it allowed to have an interstitial ad popup when there is an admob banner on the screen as well?
Following is the code for showing InterstitialAd on start off app.
import com.google.android.gms.ads.*;
public class BannerExample extends Activity {
private InterstitialAd interstitial;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
I think you should pass above code to android developers instead on web developers.
Here it is. Just insert the logic to go to your desired activity after the ad is clicked, or gives error or is canceled. This code is for when you exit the app.
An {@link Activity} that requests and can display an InterstitialAd. /
public class InterstitialAd extends Activity {
/* The log tag. */
private static final String LOG_TAG = “InterstitialSample”;
/** Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = “ca-app-pub-3448205561637334/9xxxxxx”;
/** The interstitial ad. */
private com.google.android.gms.ads.InterstitialAd interstitialAd;
/** The button that show the interstitial. */
private Button showButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.interstitial);
// Create an ad.
interstitialAd = new com.google.android.gms.ads.InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID);
loadInterstitial();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(LOG_TAG, "onAdLoaded");
showInterstitial(((ViewGroup)findViewById(android.R.id.content)).getChildAt(0));
}
@Override
public void onAdFailedToLoad(int errorCode) {
String message = String.format("onAdFailedToLoad (%s)", getErrorReason(errorCode));
Log.d(LOG_TAG, message);
finish();
}
});
}
public void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Load the interstitial ad.
interstitialAd.loadAd(adRequest);
}
/** Called when the Show Interstitial button is clicked. */
public void showInterstitial(View unusedView) {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
}
}
/** Gets a string error reason from an error code. */
private String getErrorReason(int errorCode) {
String errorReason = “”;
switch(errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = “Internal error”;
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = “Invalid request”;
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = “Network Error”;
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = “No fill”;
break;
}
return errorReason;
}