我想在3次点击后进行插页式广告

时间:2015-12-31 02:33:42

标签: android android-studio admob interstitial

我想在3次点击后显示插页式广告,现在用户点击退出按钮后会显示插页式广告。

 public final static String TAG = BaseDrawerActivity.class.getSimpleName();
//NavigationView.OnNavigationItemSelectedListener mNavigationListener;
ActionBarDrawerToggle mDrawerToggle;
InterstitialAd mInterstitialAd;

@Bind(R.id.drawerLayout) DrawerLayout mDrawerLayout;
@Bind(R.id.navigation) NavigationView mNavigationView;
@Nullable @Bind(R.id.toolbar) Toolbar mToolbar;
@Bind(R.id.adView) AdView mAdView;
@Override
public void setContentView(int layoutResID) {
    super.setContentView(R.layout.activity_drawer);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
    LayoutInflater.from(this).inflate(layoutResID, viewGroup, true);
    bindViews();
    setupAds();
}

private void setupAds(){
    if(Config.SHOULD_SHOW_ADS){
        if(Config.SHOULD_SHOW_BANNERS){
            showBannerAds();
        }

        if(Config.SHOULD_SHOW_INTERSTITIAL){
            setupInterstitial();
        }

    }
}

public void showBannerAds(){
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

private void setupInterstitial(){
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-XXXXXXXXXXXX");

    requestNewInterstitial();
}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
            .build();

    mInterstitialAd.loadAd(adRequest);
}

protected void bindViews(){
    ButterKnife.bind(this);
    setupToolbar();
}

protected void setupToolbar(){
    if(mToolbar != null){
        setSupportActionBar(mToolbar);
    }
    mNavigationView.setNavigationItemSelectedListener(mNavigationListener);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.hello_world, R.string.hello_world);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

public Toolbar getToolbar(){
    return mToolbar;
}

protected void setHamburgerButton(){
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}

@

1 个答案:

答案 0 :(得分:3)

如果您想在用户点击退出按钮(或任何按钮)三次时显示InterstitialAd,那么您需要有一个计数器变量。



int counter=1;
...
void onClick(View v){  // OR public void onBackPressed()

  if(counter==3){
    if (mInterstitialAd.isLoaded()) {
      mInterstitialAd.show();
    }
    counter=1;
  }else{
    counter++;
  }

}




还管理退出条件。