Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

admob - How to create Android interstitial ads?

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...