Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - How to show Interstitial ad correctly in this case?

I created a soundboard with a gridview Adapter (so no chance to set the Interstitial Ad on a sound button) I also have a navigationbar with three tabs, so I decidet to set the Interstitial ad on the second fragment like this:

public class SecondFragment extends Fragment {
    private InterstitialAd mInterstitialAd;

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

        final AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd = new InterstitialAd(getActivity());
        mInterstitialAd.setAdUnitId("MYID");
        mInterstitialAd.loadAd(adRequest);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }

            }

            @Override
            public void onAdClosed() {
            }
        });


        return rootView;
    }

But then the problem was that the Ad pops up with a 3 second delay, thats why I received a mail from AdMob that I have to set the Interstitial ad correctly and not like I did before.

So my question is, what would you do in this case? Where would you set the InterstitialAd?

This is my App, 3 fragments full with sound buttons: https://gyazo.com/1ecd359b38fcfe0606bb3e74b684f16e

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From your code you're giving request to load ad then when your ad is loaded then you show that ad.

There's always a delay in loading ads so my suggestion is load your ad previously is background and not show that ad. whenever you want to show ad only check is your interstitial is ready to show, if yes show.

Call createInterstitial() at once in background and after that whenever you want to show interstitial Ad, call showInterstitial().

public void crateInterstitial(){

    interstitialAd = new InterstitialAd(this.activity);
    interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // not call show interstitial ad from here            
        }

        @Override
        public void onAdClosed() {
              loadInterstitial(); 
         }
      });
    loadInterstitial();
}

public void loadInterstitial(){

     AdRequest interstitialRequest = new AdRequest.Builder().build();
     interstitialAd.loadAd(interstitialRequest);
}

public void showInterstitial(){
     if (interstitialAd.isLoaded()) 
          interstitialAd.show();                          
     else 
         loadInterstitial();                                                      
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...