How to put an Interstital Ad inside an adapter class?

前端 未结 2 599
北荒
北荒 2020-12-20 09:00

I have a ViewPager in MainActivity that open a new Activity when you touch each image that is inside an adapter.

Does anyone k

2条回答
  •  Happy的楠姐
    2020-12-20 09:10

    I have also faced this very same issue. In your case instead of opening interstitial ads from adapter activity, open it from next activity of adapter activity. Below is my Solution: Consider Activity_B as your Adapter activity.

    • Suppose there are 3 activities and opening sequence is as follows:

      Activity_A --> Activity_B --> Activity_C.
      
    • Now I want to show interstitial Ad between Activity_B and Activity_C.

    • I have first loaded the Interstitial Ad in Activity_A and then call(or show) in Activity_C.

    You can do above like this:

    In Activity_A i.e. MainActivity add code like this:

    public void showme(){
    
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
            AdRequest adRequest = new AdRequest.Builder()
                    .build();
            mInterstitialAd.loadAd(adRequest);
    
            mInterstitialAd.setAdListener(new AdListener()
            {
    
                @Override
                public void onAdClosed()
                {
                    //reload interstitial
                    AdRequest adRequest = new AdRequest.Builder()
    //                        .addTestDevice("YOUR_DEVICE_ID")
                            .build();
                    mInterstitialAd.loadAd(adRequest);
                }
            });
        }
    
        public static void showInterstitial() {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
        }
    

    Call this showme() in Activity_A inside OnCreate.

    In Activity_C paste below code inside OnCreate:

    Activity_A.showInterstitial();
    

    Also, this method doesn't violate any Google Admob policy of interstitial ads as well as banner ads.

提交回复
热议问题