How to put an Interstital Ad inside an adapter class?

前端 未结 2 596
北荒
北荒 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条回答
  • 2020-12-20 09:02

    you should do like following code.... Declare this code in adapter class at the top... private InterstitialAd mInterstitialAd;

    After this you put the rest of the code in your onBindViewHolder()

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
    
        Picasso.get()
                .load(picsArray[position])
                .into(holder.imageView);
    
        mInterstitialAd = new InterstitialAd(context);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }
    
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
            }
    
            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }
    
            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }
    
            @Override
            public void onAdClosed() {
                Intent intent = new Intent(context, Full_Image_Activity.class);
                intent.putExtra("IMG",picsArray[position]);
                context.startActivity(intent);
    

    ///// onAdClosed() method you can put your intent or what ever you want, this is for when you close the add then the intent must know what to do... } });

    }
    

    After this you can put the code in your item.onClicklistner method like below..

    itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    } else {
                        Intent intent = new Intent(context, Full_Image_Activity.class);
                        intent.putExtra("IMG",picsArray[getAdapterPosition()]);
                        context.startActivity(intent);
                    }
    
                }
            });
    

    In the else{} you can put your code like intent or whatever you want to do... Hope it will work for many peoples...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题