how to show admob interstitial on app start

2019-09-18 02:12发布

I need help how to show admob interstitial on app start i want to trigger the admon interstitial when app launched for the record my app is a swipe tab content Pleas i need any solution any one have an idea

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private InterstitialAd interstitial;

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles


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


        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("");

        //google ads Banner
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        ActionBar actionBar = getActionBar();

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-1417847946178022/7723890794");

        // Create ad request.
        adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);


        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        final ActionBar finalActionBar = actionBar;
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                finalActionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });




        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }
        });


    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }


    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }


}

2条回答
神经病院院长
2楼-- · 2019-09-18 02:38

You should not place interstitial ads on app load and when exiting apps as However. You can show Interstitial after app load, with such practice you have to implement a splash screen in your app and show interstitial after it.

An interstitial ad launches while the user is idle on the ‘Home Screen’ of the app. This implementation is in violation of our policies . One potential way to fix this violation, would be to implement a splash page that launches as the first screen of the app, before the ‘Home Screen’. The Interstitial ad should then launch in the transition between the splash screen and the ‘Home Screen’.

Reference : -- > Google Blog

查看更多
在下西门庆
3楼-- · 2019-09-18 02:56

Note that according to the disallowed admob implementations:

App load or exit

Do not place interstitial ads on app load and when exiting apps as interstitials should only be placed in between pages of app content.

Failure to adhere to this policy might lead to the disabling of ad-serving to your app.

If you still want this, you can display in your onResume's method:

boolean isAdShown;

@Override
protected void onResume() {
    super.onResume();
    if (!isAdShown) {
        displayInterstitial();
        isAdShown = true;
    }
}
查看更多
登录 后发表回答