更新后 Google 广告 SDK onAdFailedToLoad 已弃用,如何解决?

After updating Google Ads SDK onAdFailedToLoad is deprecated, How to resolve?

将 Google Ads SDK 更新到 19.3.0 后,会为 onAdFailedToLoad() 提供已弃用的警告消息。我该如何解决?

我的代码:

public void BannerAdMob() {
        final AdView adView = findViewById(R.id.adsView);
        adView.loadAd(new AdRequest.Builder().build());
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                
            }

            @Override
            public void onAdFailedToLoad(int error) { // this method is deprecated 
                
            }
        });
    }

他们添加了一个你应该重写的新方法而不是那个:

public void onAdFailedToLoad (LoadAdError adError)

您可以从 adError 对象中获取大量信息,对其调用 getCode() 将为您提供旧方法的代码。

请参考here了解更多信息。 完整示例如下:

    @Override
public void onAdFailedToLoad(LoadAdError error) {
  // Gets the domain from which the error came.
  String errorDomain = error.getDomain();
  // Gets the error code. See
  // https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
  // for a list of possible codes.
  int errorCode = error.getCode();
  // Gets an error message.
  // For example "Account not approved yet". See
  // https://support.google.com/admob/answer/9905175 for explanations of
  // common errors.
  String errorMessage = error.getMessage();
  // Gets additional response information about the request. See
  // https://developers.google.com/admob/android/response-info for more
  // information.
  ResponseInfo responseInfo = error.getResponseInfo();
  // Gets the cause of the error, if available.
  AdError cause = error.getCause();
  // All of this information is available via the error's toString() method.
  Log.d("Ads", error.toString());
}