2014-05-07 19 views
3

我想在同一活動中多次展示插頁式廣告。我正在使用下面的方法進行插頁式廣告,但它只顯示一次插頁式廣告並且從未重新加載。第2次如何調用插頁式廣告

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分鐘之後等。請幫助我如何回憶間隙並在一些圖像之後顯示它。請幫助我編寫代碼或編輯我的代碼。我完全不知道如何才能完成這項任務。我會非常感謝。

回答

12

修改您的AdListener的代碼

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(); 
+0

非常非常感謝名單回覆。 lastTimeAdFail&lastTimeAdShown都顯示錯誤。等待您的回覆。 – HBBR

+0

謝謝,我編輯的代碼添加2個變量作爲類變量。請立即嘗試 – essess

+0

如果您想在第一時間即時顯示廣告,可以將這兩個變量設置爲0 – essess