2015-02-11 165 views
0

我檢查其他話題有關SOF,並尋找一些新的知識。如何刪除延遲展示在此代碼AdMob Intersitial廣告刪除延遲

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) { 
    // Create the interstitial 
    interstitial = new InterstitialAd(this); 
    interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id)); 

    // Create ad request. 
    adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
} 

//initialise banner ad 
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.BannerAd_unit_id); 
showBanner(); 
} 

public void openAd() { 
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0)   { 
    runOnUiThread(new Runnable() { 
     public void run() { 
      if (!interstitial.isLoaded()) { 
       interstitial.loadAd(adRequest); 
      } 
      interstitial.setAdListener(new AdListener() { 
       public void onAdLoaded() { 
        interstitial.show(); 
       } 

      }); 

     } 
    }); 
} 
} 

這裏的淵源發佈AdMob聯播間質性廣告:How to make Admob interstitial show without delay?

m newbie and just learning programming, that就是爲什麼我真的問例如在此代碼:)

編輯: 這就是openAd被稱爲的方式

public synchronized void GameOver() { 
    if (lives_left > 0) { 
     //success! - game passed - save score 
     ScoreManager.save_localscore_simple(score_times[currentLevel], "" + currentLevel, false); 

     if (Level_Buttons.length >= currentLevel + 1) 
      ScoreManager.save_localscore_simple(unlocked, "unlock" + (currentLevel + 1)); 

     if (sound_success != 0 && !sound_muted) 
      sp.play(sound_success, 1, 1, 0, 0, 1); 

     //intersitial ad 
     ad_counter++; 
     if (ad_counter >= getResources().getInteger(R.integer.ad_shows_every_X_gameovers)) { 
      openAd(); 
      ad_counter = 0; 
     } 


    } else { 
     //game not passed 
     if (sound_gameover != 0 && !sound_muted) 
      sp.play(sound_gameover, 1, 1, 0, 0, 1); 
    } 


    //open interstitial ad 
    ad_counter++; 
    if (ad_counter >= getResources().getInteger(R.integer.ad_shows_every_X_gameovers)) { 
     openAd(); 
     ad_counter = 0; 
    } 

回答

1

你的代碼會做你所要求的。但它是而不是你應該做的,因爲它會惹惱你的用戶,並可能禁止你的Admob帳戶。

你應該做的是早點打電話interstitial.loadAd(),可能在你的onCreate()

public void onCreate() { 
    // Create the interstitial 
    interstitial = new InterstitialAd(this); 
    interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id)); 

    interstitial.setAdListener(new AdListener() { 
     public void onAdClosed() { 
      // Create another ad request. 
      final AdRequest adRequest = new AdRequest.Builder() 
       .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
       .build(); 
      interstitial.loadAd(adRequest); 
     } 
    }); 
    // Create ad request. 
    final AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
    interstitial.loadAd(adRequest); 
} 

然後在你的應用(在GAMEOVER)天然破發點,叫 if (interstitial.isLoaded()) { interstitial.show(); }

一旦從顯示廣告(在AdListener.onAdClosed()您可以撥打interstitial.loadAd()和重複返回。

+0

哦,威廉,我希望你會回答我的問題,你在這裏:)謝謝!但是你能否解釋我一點,因爲我正在學習,而且我很困惑。我必須如何更改我的代碼才能工作?我嘗試了所有方法,但每次出現錯誤。 – Karnak 2015-02-11 23:32:30