如何显示插页式广告?

时间:2017-03-31 20:39:27

标签: java android admob advertising interstitial

我有一个带有三个标签的导航栏,每个标签都是一个片段。 现在我想要,如果我点击第二个标签,就会弹出插页式广告。 那么我在哪里.loadAd广告?我需要在哪里播放广告?

我的主要活动:

public class MainActivity extends AppCompatActivity{
    public MediaPlayer mp;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    public InterstitialAd mInterstitialAd;

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


        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();



        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);



        AdRequest adRequest = new AdRequest.Builder().build();

        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd = new InterstitialAd(getContext());
        mInterstitialAd.setAdUnitId("ca-app-pub-9756227769488050/2954649723");
        mInterstitialAd.loadAd(adRequest);


    }

我的第二个片段:

public class SecondFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.second_layout,container,false);


        return rootView;
    }

而且(我不知道这是否重要)我的TabFragment:

public class TabFragment extends Fragment {
    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        /**
         *Inflate tab_layout and setup Views.
         */
            View x =  inflater.inflate(R.layout.tab_layout,null);
            tabLayout = (TabLayout) x.findViewById(R.id.tabs);
            viewPager = (ViewPager) x.findViewById(R.id.viewpager);

        /**
         *Set an Apater for the View Pager
         */
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        /**
         * Now , this is a workaround ,
         * The setupWithViewPager dose't works without the runnable .
         * Maybe a Support Library Bug .
         */

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                   }
        });

        return x;

    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * Return fragment with respect to Position .
         */

        @Override
        public Fragment getItem(int position)
        {

            if(position == 0){
                return new FirstFragment();
            }
            if(position == 1){
                return new SecondFragment();
            }
            if(position == 2){
                return new ThirdFragment();
            }

        return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }

        /**
         * This method returns the title of the tab according to the position.
         */

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "first";
                case 1 :
                    return "second";
                case 2 :
                    return "third";
            }
                return null;
        }
    }


        });
    }

}

那我在哪里说showInterstitial()

final public void showInterstitial(){
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if(mInterstitialAd.isLoaded()){
                mInterstitialAd.show();
            }

        }

        @Override
        public void onAdClosed() {
        }

我现在挂了这个问题一个多月了,所以请详细解释一下。感谢

2 个答案:

答案 0 :(得分:0)

您应该将load()保留在主活动中,就像您一样,它将开始缓存广告。然后,您将调用showInterstitial()函数来处理适配器单击事件,即处理片段的位置。

答案 1 :(得分:0)

public class MainActivity extends Activity {
private InterstitialAd interstitial;

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


mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);

    // Prepare the Interstitial Ad
    interstitial = new InterstitialAd(MainActivity.this);
    // Insert the Ad Unit ID
    interstitial.setAdUnitId("ca-app-pub-123456789/123456789");

    //Locate the Banner Ad in activity_main.xml
    AdView adView = (AdView) this.findViewById(R.id.adView);

    // Request for Ads
    AdRequest adRequest = new AdRequest.Builder().build();

    // Load ads into Banner Ads
    adView.loadAd(adRequest);


}
public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
    // Load ads into Interstitial Ads
    interstitial.loadAd(adRequest);

    // Prepare an Interstitial Ad Listener
    interstitial.setAdListener(new AdListener() {
        public void onAdLoaded() {
            if (interstitial.isLoaded()) {
        interstitial.show();
    }
        }
    });
    }
}