Search code examples
androidadmobinterstitial

how to call interstitial 2nd time


I want to show interstitial in same activity more than one time. I am using below method for interstitial but it show interstitial only one time and never reloaded.

  int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on};
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState); 
       setContentView(R.layout.main);
    // for interstitial
     interstitial = new InterstitialAd ( this ); 
     interstitial.setAdUnitId (my ad id);
    AdRequest.Builder adRequestBuilder=new AdRequest.Builder ();

    interstitial.setAdListener ( new AdListener (){
    }
    });
   interstitial.loadAd ( adRequestBuilder.Build ());

        hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
         iButton = (Button) findViewById(R.id.bNext); 
         gButton = (Button) findViewById(R.id.bPrev); 
       //Just set one Click listener for the image 
        iButton.setOnClickListener(iButtonChangeImageListener); 
         gButton.setOnClickListener(gButtonChangeImageListener); 
        }
         View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
         public void onClick(View v) {
        //Increase Counter to move to next Image 
          currentImage++;
         currentImage = currentImage % images.length; 
       hImageViewPic.setImageResource(images[currentImage]); 

         / / here to show interstitial
          if (interstitial. isLoaded ()){
              interstitial.show ();
           }
         } }; 
        View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
         public void onClick(View v) { 
         //Increase Counter to move to next Image 
           currentImage--; 
            currentImage = (currentImage + images.length) % images.length; 
           hImageViewPic.setImageResource(images[currentImage]);
           } 
           };

As I pressed button then interstitial shows. I want to show interstitial as some images viewed. For example after 10th and 20th and so on or after 5 minutes etc. Kindly help me how can I recall interstitial and show it after some images. Kindly help me with code or edit my code. I have completely no idea how can I achieve this task. I'll be very thankfull.


Solution

  • Modify your adlistener code to

    interstitial.setAdListener(new AdListener() {
    
    
     @Override
          public void onAdLoaded() {
    
          }
    
          @Override
          public void onAdFailedToLoad(int errorCode) {
                long timestamp = System.currentTimeMillis();
                if(timestamp > lastTimeAdFail + 120*1000)
                {
                  AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
    
                    // Load the interstitial ad again
                interstitial.loadAd(adRequest);
                }
    
          }
    
          @Override
          public void onAdClosed () 
          {
              AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
    
                // Load the interstitial ad again
              interstitial.loadAd(adRequest);
          }
        });
    

    Also modify your onclick listener to

    View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
             public void onClick(View v) {
            //Increase Counter to move to next Image 
              currentImage++;
             currentImage = currentImage % images.length; 
           hImageViewPic.setImageResource(images[currentImage]); 
     if (interstitial.isLoaded()) {
                                long timestamp = System.currentTimeMillis();
    
                                    if(timestamp > lastTimeAdShown + 300*1000)
                                    {
                                        interstitial.show();
    
                                        lastTimeAdShown = timestamp;
                                    }
    
                                }
                                else 
                                {
                                    long timestamp = System.currentTimeMillis();
                                    if(timestamp > lastTimeAdFail + 120*1000)
                                    {
                                        AdRequest adRequest = new AdRequest.Builder()
                                        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
    
                                        // Load the interstitial ad.
                                        interstitial.loadAd(adRequest);
    
                                        lastTimeAdFail = timestamp;
                                    }
                                }
    
             } }; 
    

    EDIT: Define two class variables as

    private long lastTimeAdShown=System.currentTimeMillis();
    
    private long lastTimeAdFail=System.currentTimeMillis();