如何第二次调用插页式广告

3
我想在同一个Activity中多次显示插屏广告。我正在使用下面的方法来显示插屏广告,但它只显示一次,之后就不再重新加载了。
  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]);
           } 
           };

当我按下按钮时,插页式广告会显示出来。我想在查看某些图片后显示插页式广告。例如,在查看第10张、第20张等之后或者在5分钟后等等。请帮助我如何调用插页式广告并在查看一些图片后显示它。请为我提供代码或编辑我的代码。我完全不知道如何完成这个任务。非常感谢您的帮助。
1个回答

12

修改您的广告监听器代码为

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);
      }
    });

同时修改您的 onclick 监听器为:

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;
                                }
                            }

         } }; 

编辑:定义两个类变量为

private long lastTimeAdShown=System.currentTimeMillis();

private long lastTimeAdFail=System.currentTimeMillis();

非常感谢您的回复。lastTimeAdFail和lastTimeAdShown都显示错误。期待您的回复。 - HBBR
谢谢,我编辑了代码,将2个变量添加为类变量。请现在尝试。 - essess
如果您想在第一时间立即显示广告,可以将这两个变量都设置为0。 - essess
我无言以对,谢谢你。非常非常非常感谢。爱你哥们。 - HBBR
好的,我会使用它!adRequest 可以成为一个成员变量,使代码更加简洁。 - WindRider

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接