Android:带广告的按钮(onAdLoaded)

时间:2014-02-03 05:19:09

标签: android button visible interstitial

我正在向“活动”添加广告,以便在广告加载完成之前将转到下一个“活动”的按钮设置为“不可见”。

public void showAd_andSetGoButton()   
{   
    if (isNetworkAvailable() == true)
    {
        button_go.setVisibility(View.INVISIBLE);    
        button_loading.setVisibility(View.VISIBLE);

        mPublisherInterstitialAd = new PublisherInterstitialAd(this);
        mPublisherInterstitialAd.setAdUnitId(MY_PUBLISHER_ID);

        // Create an ad request.
        PublisherAdRequest.Builder publisherAdRequestBuilder = new PublisherAdRequest.Builder();

        // Optionally populate the ad request builder.
        publisherAdRequestBuilder.addTestDevice(PublisherAdRequest.DEVICE_ID_EMULATOR);

        // Set an AdListener.
        mPublisherInterstitialAd.setAdListener(new AdListener() 
        {
           @Override
           public void onAdLoaded() 
           {
                // Toast.makeText(First.this, "The interstitial is loaded", Toast.LENGTH_SHORT).show();             
               button_go.setVisibility(View.VISIBLE);   
               button_loading.setVisibility(View.INVISIBLE);
           }

           @Override
           public void onAdClosed() 
           {
                go_action();
           }
       });

       // Start loading the ad now so that it is ready by the time the user is ready to go to the next level.
       mPublisherInterstitialAd.loadAd(publisherAdRequestBuilder.build());          
    }
    else
    {
        button_go.setVisibility(View.VISIBLE);  
    }
}  

问题:

以上代码运作良好。但是,我知道有时即使发送广告请求,也不会因缺少广告资源等原因而退回广告。我想问一下这种情况何时出现,onAdLoaded()是否会被正确退回,以便在没有广告加载的情况下可以看到button_go?

谢谢!

1 个答案:

答案 0 :(得分:0)

AdListener()

中使用此覆盖功能

这会在广告无法接收时触发

mPublisherInterstitialAd.setAdListener(new AdListener() 
{
   @Override
   public void onAdLoaded() 
   {
       // Toast.makeText(First.this, "The interstitial is loaded", Toast.LENGTH_SHORT).show();             
       button_go.setVisibility(View.VISIBLE);   
       button_loading.setVisibility(View.INVISIBLE);
   }

   @Override
   public void onAdClosed() 
   {
       go_action();
   }

   @Override
   public void onFailedToReceiveAd(Ad arg0, ErrorCode error) {
     // add your code here 
   }    
});