Admob: Interstitial ads showing every time

2019-09-17 10:07发布

i'm finishing a video application and i'm displaying interstitial ads when leaving the video activity. I just want to show it once every X minutes, but it seems to be showing every time i leave that screen.

This is my activity code.

OnCreate:

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle b = getIntent().getExtras();
  videoId = b.getString("videoId");
  setContentView(R.layout.youtube_player);

  interstitialAd= new InterstitialAd(this);
  interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
  AdRequest adRequest = new AdRequest.Builder().build();
  interstitialAd.loadAd(adRequest);
  etc...

OnBackPressed:

@Override
public void onBackPressed() {
    ShowAds();

}

private void ShowAds() {
    if (interstitialAd.isLoaded()) {
        interstitialAd.show();
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                finish();
            }
        });
    }else{
        super.onBackPressed();
    }
}`

of course, it is setted like this into AdMob:

Note: my application is not published, so it is showing the "preview" o "sample". I am using my ad unit ID:

enter image description here

thanks,

1条回答
孤傲高冷的网名
2楼-- · 2019-09-17 11:08

Two options :

  1. Interstitial ads show when I am leaving current Activity but only If I completed X minute on current Activity.

    boolean isAdShow=false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        int minute=1;  // X minute
    
        isAdShow=false;
        new CountDownTimer(minute*60000,1000) {
          @Override
          public void onTick(long millisUntilFinished) {
    
          }
    
          @Override
          public void onFinish() {
              isAdShow=true;
          }
      }.start();
    }
    
    private void ShowAds() {
    
       if (interstitialAd.isLoaded() && isAdShow) {
           interstitialAd.show();
           interstitialAd.setAdListener(new AdListener() {
              @Override
              public void onAdClosed() {
                  super.onAdClosed();
                  finish();
              }
           });
        }else{
           super.onBackPressed();
        }
    }
    
  2. Don't wait for user to press back button and leave current Activity, just call ShowAds() from onFinish() method of Timer.

I'll recommend to use 1st one because it not violate AdMob Ad policy and user experience too.

EDIT

You can also use X times counter, like when X = 3 i.e After 3 times call of onCreate() method make eligible to show Ad.

public static int adCounter;
public static final int TIME_COUNTER=3;  //Should be always greater than zero

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    adCounter++;
}

private void ShowAds() {

  if (interstitialAd.isLoaded() && adCounter%TIME_COUNTER==0) {
    interstitialAd.show();
    interstitialAd.setAdListener(new AdListener() {
       @Override
       public void onAdClosed() {
           super.onAdClosed();
           finish();
       }
    });
   }else{
    super.onBackPressed();
  }
}
查看更多
登录 后发表回答