【问题标题】:How to show the interstitial ads on second click?如何在第二次点击时显示插页式广告?
【发布时间】:2018-12-31 10:50:47
【问题描述】:

任何人都知道如何在用户第二次单击按钮时显示插页式广告。我的意思是当用户单击一次按钮时,广告不应该出现,但每当用户第二次单击同一个按钮时,广告必须显示..

public class MainActivity extends AppCompatActivity {


Toolbar toolbar;
ShowInterstitial showInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showInterstitial = new ShowInterstitial(this);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.app_bar);

    /*toolbar = findViewById(R.id.app_bar);
    toolbar.setTitle("hell");
    toolbar.*/

}
int counter = 0;
public void onClick(View view) {

    if(view.getId() == R.id.ll1 ) {


        counter++;
        if (counter == 2) {
            counter = 0;
            Intent intent = new Intent(this, AggregatesActivity.class);
            startActivity(intent);
            if (showInterstitial != null && ShowInterstitial.isLoaded())
                showInterstitial.showInterstitial();
        }

    }

和 ShowInterstitial 代码在这里,我在不同的活动中调用。

    public class ShowInterstitial {

    private InterstitialAd mInterstitialAd;
    private Context context;
    private boolean isAddReplace = false;

    public ShowInterstitial(Context context) {
        this.context = context;
        mInterstitialAd = newInterstitialAd(context);
        loadInterstitial();
    }

    private InterstitialAd newInterstitialAd(final Context context) {
        InterstitialAd interstitialAd = new InterstitialAd(context);
        /*if (!isAddReplace)
            interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));*/

            interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                isAddReplace = !isAddReplace;
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
            }

            @Override
            public void onAdClosed() {
                goToNextLevel();
            }
        });
        return interstitialAd;
    }

    public boolean showInterstitial() {
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            goToNextLevel();
        }
        return false;
    }

    public void loadInterstitial() {
        // Disable the next level button and load the ad.
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        mInterstitialAd.loadAd(adRequest);
    }

    private void goToNextLevel() {
        // Show the next level and reload the ad to prepare for the level after.

        mInterstitialAd = newInterstitialAd(context);
        loadInterstitial();
    }


}

【问题讨论】:

  • 只需使用int 计数器并在按钮单击时将其递增检查第二次点击并显示广告。
  • 如果你使用这个那么请你提供代码吗?

标签: android interstitial


【解决方案1】:

@Sufyan Hashmi 您需要一个 int 变量,只要值为 2,其值就会在每次点击时增加,您应该调用 load inerestitial ad 并将变量的值分配为零。

 int counter = 0;

 if(view.getId()==R.id.ll1)

    {
        counter++;
        if (counter == 2) {
            counter = 0;
            Intent intent = new Intent(this, AggregatesActivity.class);
            startActivity(intent);
            if (showInterstitial != null && showInterstitial.isLoaded())
                showInterstitial.showInterstitial();
        }

    }

【讨论】:

  • 请确认您要先显示 InterstitialAd,然后广告关闭然后活动打开?或者您想同时打开活动和插页式广告?
  • 同时......当用户在按钮上粘贴时,它总是转到下一个活动,但是当用户第二次粘贴相同的按钮时,广告必须显示。
  • @Sufyan Hashmi 我已经编辑了代码。请查看。
  • 我正面临这个问题。实际上我已经为广告创建了一个名为 ShowInterstitial 的单独类,我认为这就是为什么现在 M 面对 .isLoaded。我还编辑了问题,请查看。
  • @Sufyan Hashmi 请分享您的 ShowInterstitial 课程。
【解决方案2】:

如何在android中第二次点击显示插页式广告?

您可以使用boolean 变量并根据该boolean 变量管理点击事件

示例: 采用 boolean 变量和 true 值 当用户单击button 时,比在ClickListener 内部检查boolean 变量是否为true 意味着使用第一次单击按钮 并将boolean变量的值更改为false

示例代码

取一个boolean变量

boolean isFirstTimeClick=true;

现在让你的ClickListener 像这样

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(isFirstTimeClick){
                isFirstTimeClick=false;
                Toast.makeText(PicturePreviewActivity.this, "First Time Click", Toast.LENGTH_SHORT).show();
            }else {
                isFirstTimeClick=true;
                Toast.makeText(PicturePreviewActivity.this, "Second Time Click", Toast.LENGTH_SHORT).show();
            }
        }
    });

【讨论】:

  • 我做了这个并将其粘贴到我的代码中但没有用.. 这是我的代码 if(view.getId() == R.id.ll1 && isFirstTimeClick) { isFirstTimeClick=true; Intent intent = new Intent(this, AggregatesActivity.class);开始活动(意图); showInterstitial.showInterstitial();
  • @SufyanHashmi 请更新您的代码,以便我可以帮助您
  • 看我的问题
  • @SufyanHashmi 分享你的整个代码,并解释你想在哪里为第二个代码逻辑
  • 我只想在第二次点击时显示...单击或默认情况下它工作正常,但我想添加一些代码以在第二次点击时显示它..
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
相关资源
最近更新 更多