2014-03-01 82 views
4

我想检查AdView是否确实加载了广告并且是可见的,因此需要在显示屏上留出空间,或者如果互联网连接不可用时没有加载广告。 如果它没有加载广告,我可以使用该空间作为别的东西。如何检查AdView是否可见?

如何做到这一点?

回答

5

您可以为此执行AdListener。 只需覆盖onAdFailedToLoadonAdLoaded

1
mAdView = (AdView) findViewById(R.id.adView); 
mAdView.setAdListener(new AdListener() { 
    // Called when an ad is loaded. 
    @Override 
    public void onAdLoaded() { 
     Log.e(TAG, "Google onAdLoaded"); 
    } 

    // Called when an ad failed to load. 
    @Override 
    public void onAdFailedToLoad(int error) { 
     String message = "Google onAdFailedToLoad: " + getErrorReason(error); 
     Log.e(TAG, message); 
    } 

    // Called when an Activity is created in front of the app 
    // (e.g. an interstitial is shown, or an ad is clicked and launches a new Activity). 
    @Override 
    public void onAdOpened() { 
     Log.e(TAG, "Google onAdOpened"); 
    } 

    // Called when an ad is clicked and about to return to the application. 
    @Override 
    public void onAdClosed() { 
     Log.e(TAG, "Google onAdClosed"); 
    } 

    // Called when an ad is clicked and going to start a new Activity that will leave the application 
    // (e.g. breaking out to the Browser or Maps application). 
    @Override 
    public void onAdLeftApplication() { 
     Log.d(TAG, "Google onAdLeftApplication"); 
    } 
}); 
mAdRequest = new AdRequest.Builder().build(); 
mAdView.loadAd(mAdRequest); 


private String getErrorReason(int errorCode) { 
    // Gets a string error reason from an error code. 
    String errorReason = ""; 
    switch (errorCode) { 
     case AdRequest.ERROR_CODE_INTERNAL_ERROR: 
      errorReason = "Internal error"; 
      break; 
     case AdRequest.ERROR_CODE_INVALID_REQUEST: 
      errorReason = "Invalid request"; 
      break; 
     case AdRequest.ERROR_CODE_NETWORK_ERROR: 
      errorReason = "Network Error"; 
      break; 
     case AdRequest.ERROR_CODE_NO_FILL: 
      errorReason = "No fill"; 
      break; 
    } 
    return errorReason; 
}