Skip to content

Commit 696cb2d

Browse files
malandr2copybara-github
authored andcommitted
Added onAdFailedToLoad snippet
PiperOrigin-RevId: 815717842
1 parent 0563d42 commit 696cb2d

File tree

2 files changed

+63
-0
lines changed
  • java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets
  • kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets

2 files changed

+63
-0
lines changed

java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
package com.google.android.gms.snippets;
1616

17+
import android.util.Log;
1718
import android.view.View;
1819
import android.view.ViewGroup;
1920
import androidx.annotation.NonNull;
21+
import com.google.android.gms.ads.AdError;
2022
import com.google.android.gms.ads.AdListener;
2123
import com.google.android.gms.ads.AdView;
2224
import com.google.android.gms.ads.LoadAdError;
25+
import com.google.android.gms.ads.ResponseInfo;
2326
import com.google.android.gms.ads.admanager.AdManagerAdView;
2427
import com.google.android.gms.ads.admanager.AppEventListener;
2528

@@ -89,6 +92,36 @@ public void destroyBanner() {
8992

9093
// [END destroy]
9194

95+
public void handleOnAdFailedToLoad(AdView adView) {
96+
adView.setAdListener(
97+
new AdListener() {
98+
// [START handle_on_ad_failed_to_load]
99+
@Override
100+
public void onAdFailedToLoad(@NonNull LoadAdError adError) {
101+
// Gets the domain from which the error came.
102+
String errorDomain = adError.getDomain();
103+
// Gets the error code. See
104+
// https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
105+
// for a list of possible codes.
106+
int errorCode = adError.getCode();
107+
// Gets an error message.
108+
// For example "Account not approved yet". See
109+
// https://support.google.com/admob/answer/9905175 for explanations of
110+
// common errors.
111+
String errorMessage = adError.getMessage();
112+
// Gets additional response information about the request. See
113+
// https://developers.google.com/admob/android/response-info
114+
// information.
115+
ResponseInfo responseInfo = adError.getResponseInfo();
116+
// Gets the cause of the error, if available.
117+
AdError cause = adError.getCause();
118+
// All of this information is available using the error's toString() method.
119+
Log.d("Ads", adError.toString());
120+
}
121+
// [END handle_on_ad_failed_to_load]
122+
});
123+
}
124+
92125
public void manualImpressionCounting() {
93126
// [START enable_manual_impressions]
94127
if (adManagerAdView != null) {

kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.android.gms.snippets
1616

17+
import android.util.Log
1718
import android.view.ViewGroup
1819
import com.google.android.gms.ads.AdListener
1920
import com.google.android.gms.ads.AdView
@@ -77,6 +78,35 @@ private class BannerSnippets : AppEventListener {
7778

7879
// [END destroy]
7980

81+
fun handleOnAdFailedToLoad() {
82+
adView?.adListener =
83+
object : AdListener() {
84+
// [START handle_on_ad_failed_to_load]
85+
override fun onAdFailedToLoad(error: LoadAdError) {
86+
// Gets the domain from which the error came.
87+
val errorDomain = error.domain
88+
// Gets the error code. See
89+
// https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
90+
// for a list of possible codes.
91+
val errorCode = error.code
92+
// Gets an error message.
93+
// For example "Account not approved yet". See
94+
// https://support.google.com/admob/answer/9905175 for explanations of
95+
// common errors.
96+
val errorMessage = error.message
97+
// Gets additional response information about the request. See
98+
// https://developers.google.com/admob/android/response-info
99+
// information.
100+
val responseInfo = error.responseInfo
101+
// Gets the cause of the error, if available.
102+
val cause = error.cause
103+
// All of this information is available using the error's toString() method.
104+
Log.d("Ads", error.toString())
105+
}
106+
// [END handle_on_ad_failed_to_load]
107+
}
108+
}
109+
80110
fun manualImpressionCounting() {
81111
// [START enable_manual_impressions]
82112
adManagerAdView?.setManualImpressionsEnabled(true)

0 commit comments

Comments
 (0)