如何正确加载插页式广告?

时间:2018-06-05 20:43:56

标签: android admob interstitial

我已编写代码在我的Android应用中显示插页式广告。它的工作正常,但问题是广告突然爆发了。可能是什么问题呢??是否与用户过于频繁地看到广告的方式相同?

MainActivity

  public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

            private ViewPager viewPager;
            private DrawerLayout drawer;
            private TabLayout tabLayout;
            private String[] pageTitle = {"fragment1", "fragment2","Fragment3"};

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

  InterstitialAd mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_id));
                AdRequest adRequestInter = new AdRequest.Builder().build();
                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                    }
                    @Override
                    public void onAdClosed()
                    {
                        AdRequest adRequest = new AdRequest.Builder()
                                .build();
                        mInterstitialAd.loadAd(adRequest);
                    }
                });
                mInterstitialAd.loadAd(adRequestInter);
                viewPager = (ViewPager) findViewById(R.id.view_pager);
                viewPager.setOffscreenPageLimit(3);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                drawer = (DrawerLayout) findViewById(R.id.drawerLayout);

                setSupportActionBar(toolbar);

                AppRate.with(this)
                        .setInstallDays(0)
                        .setLaunchTimes(3)
                        .setRemindInterval(2)
                        .monitor();

                AppRate.showRateDialogIfMeetsConditions(this);
                AppRate.with(this).clearAgreeShowDialog();

                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                        R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                tabLayout = (TabLayout) findViewById(R.id.tab_layout);
                for (int i = 0; i < 3; i++) {
                    tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
                }

                tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                assert navigationView != null;
                navigationView.setNavigationItemSelectedListener(this);
                navigationView.setItemIconTintList(null);

                ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(pagerAdapter);

                viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

                tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        viewPager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });


            }

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();

                if (id == R.id.fr1) {
                    viewPager.setCurrentItem(0);
                } else if (id == R.id.fr2) {
                    viewPager.setCurrentItem(1);
                }else if (id == R.id.fr3) {
                    viewPager.setCurrentItem(2);
                }

                else if (id == R.id.Suggestion) {
                    Intent intent = new Intent(this, Suggestion.class);
                    startActivity(intent);
                }
                else if (id == R.id.Logic) {
                    Intent intent = new Intent(this, logicBehind.class);
                    startActivity(intent);
                }
                else if (id == R.id.close) {
                    finish();
                }

                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
            @Override
            public void onBackPressed() {
                boolean wentBack = false;

                if (Fragment1.canGoBack()) {
                    wentBack = true;
                    Fragment1.goBack();
                }
                if (Fragment2.canGoBack()) {
                    wentBack = true;
                    Fragment2.goBack();
                }
                if (Fragment3.canGoBack()) {
                    wentBack = true;
                    Fragment3.goBack();
                }


                if (!wentBack) {
                    new AlertDialog.Builder(this)
                            .setTitle("Exit app")
                            .setMessage("Do you want to exit the app?")
                            .setIcon(R.mipmap.mainlogo)
                            .setPositiveButton("Yes", (dialog, which) -> super.onBackPressed())
                            .setNegativeButton("No", (dialog, which) -> {})
                            .show();
                }
            }


            @Override
               public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return super.onCreateOptionsMenu(menu);


           }

        }

这是我尝试过的。插页式广告的代码添加在MainActivity.java的onCreate方法中。

1 个答案:

答案 0 :(得分:0)

 mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                    }
                    @Override
                    public void onAdClosed()
                    {
                        AdRequest adRequest = new AdRequest.Builder()
                                .build();
                        mInterstitialAd.loadAd(adRequest);
                    }
                });
                mInterstitialAd.loadAd(adRequestInter);

根据您编写的代码,您可以在adClose()中调用loadAd()。因此,当您点击十字时,会加载下一个广告。你在onAdLoaded中调用了ad.show()。因此,只要广告加载,它就会显示..所以这就像一个连续的循环,下一个广告将在每个广告关闭时显示。