I tried things from many blogs but none gave a step-by-step solution. Should I edit something on the AdMob site? I created the site from the ad sit/app option under the Sites & Apps tab.
I used this code:
interstitial = new InterstitialAd(this, "MyAdMobID");
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
// Create ad request
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
// Begin loading your interstitial
interstitial.loadAd(adRequest);
adRequest.setTesting(true);
Using the last Android framework, I figured out that I need to call the load() function each time the ad is closed.
import com.google.android.gms.ads.*;
import android.os.Handler;
import android.os.Looper;
import android.app.Activity;
class MyActivity extends Activity implements AdListener {
private InterstitialAd adView; // The ad
private Handler mHandler; // Handler to display the ad on the UI thread
private Runnable displayAd; // Code to execute to perform this operation
@Override
public void onCreate(Bundle savedInstanceState) {
adView = new InterstitialAd(mContext);
adView.setAdUnitId("ca-app-pub-XXXXXXXXXX");
adView.setAdListener(this);
mHandler = new Handler(Looper.getMainLooper());
displayAd = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (adView.isLoaded()) {
adView.show();
}
}
});
}
};
loadAd();
}
@Override
public void onAdClosed() {
loadAd(); // Need to reload the Ad when it is closed.
}
void loadAd() {
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Load the adView object witht he request
adView.loadAd(adRequest);
}
//Call displayInterstitial() once you are ready to display the ad.
public void displayInterstitial() {
mHandler.postDelayed(displayAd, 1);
}
}
Call this function on app's opening:
InterstitialAd interstitial;
public void AdMob() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("YOUR_TEST_DEVICE_ID").build();
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("YOUR_AD_ID");
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
//Ads loaded
}
@Override
public void onAdClosed() {
super.onAdClosed();
//Ads closed
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
//Ads couldn't loaded
}
});
interstitial.loadAd(adRequest);
}
Then you can show ads:
if (interstitial.isLoaded()){
interstitial.show();
}
You should prepare ads before showing it. These took 3-5 seconds depending internet speed of device.
Unlike banners, insterstitial ads don't automatically show once their loaded. You'll have to listen for AdMob's onReceiveAd()
callback, and inside that callback, call interstital.show()
to show your interstitial.
public YourActivity extends Activity implements AdListener {
...
@Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
Check out a code example here. This example will show the interstitial as soon as it is received. Alternatively, you may want to wait until a proper time to show the interstitial, such as at the end of a game level, and you could check interstitial.isReady()
to see if you can show the interstitial.
You cant implement AdListener anymore, I used it this way:
final InterstitialAd mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
});
mInterstitialAd.loadAd(adRequestInter);
Place your own ID in strings.xml named interstitial_ad_unit_id, or replace
getResources().getString(R.string.interstitial_ad_unit_id)
with your ID.
I think this example code will help you out.
How to Add AdMob Interstitial Ads in Your Android Apps
In this example, it shows you the whole source code. And it also provides some solutions for common errors. For example, onFailedToReceiveAd error solution and no ad returned solution. You can also download the source code from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With