Admob: Interstitial ads showing every time

巧了我就是萌 提交于 2019-12-25 08:36:33

问题


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:

thanks,


回答1:


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


来源:https://stackoverflow.com/questions/44120462/admob-interstitial-ads-showing-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!