0

我在showAd()里面OnResume打电话要求 interstitialAd。

显示广告()

public void showAd()   
{   
    SavedFrequency = getSharedPreferences("adfreq", MODE_PRIVATE);
    AdfrequencyInt = SavedFrequency.getInt("adfreq", 0);
    AdfrequencyInt++;
    if (AdfrequencyInt > 59) 
    {
        AdfrequencyInt = 0;
    }
    Toast.makeText(this, ""+AdfrequencyInt, Toast.LENGTH_SHORT).show(); 
    SharedPreferences.Editor preferencesEditorF1 = SavedFrequency.edit();
    preferencesEditorF1.putInt("adfreq", AdfrequencyInt);
    preferencesEditorF1.apply();

    if (AdfrequencyInt % 10 == 0) 
    {
        interstitialAd = new InterstitialAd(this, MY_PUBLISHER_ID); // Create an ad
        interstitialAd.setAdListener(this); // Set the AdListener.
        AdRequest adRequest = new AdRequest();
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
        interstitialAd.loadAd(adRequest);
    }
}   

问题:

广告可以显示,但通常会延迟,即当我已经开始另一个活动时,它仍在后台加载,然后突然弹出广告。我想问一下如何先加载广告并将其存储在后台,以便仅在我返回此活动时才显示广告?

谢谢!

4

2 回答 2

0

这种方式应该会更好,如在谷歌开发者网站中找到的那样:http: //developer.android.com/reference/com/google/android/gms/ads/InterstitialAd.html

     mNextLevelButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             // Show the interstitial if it is ready. Otherwise, proceed to the next level
             // without ever showing it.
             if (mInterstitialAd.isLoaded()) {
                 mInterstitialAd.show();
             } else {
                 // Proceed to the next level.
                 goToNextLevel();
             }
         }

如果加载成功,则显示,否则直接进入另一个活动。

于 2014-01-26T10:57:41.750 回答
0

一种方法是打开广告活动,将其置于后台并在其上加载另一个活动,因此实际上有两个活动,广告一个在后台加载。这将通过以下方式完成:

final Intent intent = new Intent(this, AnotherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);  

我不知道这将如何在您的代码中实际工作,因为我不知道延迟有多少,但您可以使用 aCountDownLatch在后台加载之前同步广告显示。该CountDownLatch 页面有一个很好的示例,说明如何将其用作信号量(基本上,将其声明为CountDownLatch(1))。您可以countDown()在广告已经加载时调用,这意味着 Activity 可能已经显示。

于 2014-01-22T12:48:39.407 回答