1
votes

getwindowmanager().getdefaultdisplay() and display.getMetrics are deprecated

using this code, for Android 11 ,

private AdSize getAdSize() {
   DisplayMetrics outMetrics = this.getResources().getDisplayMetrics();
 }

It works well and shows ads if target sdk is set to 29. But, if changed to target sdk to 30, it shows no ads.

When the targetSdkVersion is 30, ads failed to load with error code 0.

2
Have you found a solution? - TimWeb
I have the same problem that adaptive banner ads not working on API30 . Any solution yet ? - Frank

2 Answers

1
votes

Can you try this? It should give the same output from the code snippet in documentation itself

    private AdSize getAdSize() {

    DisplayMetrics outMetrics = getResources().getDisplayMetrics();

    float widthPixels = getScreenWidth();
    float density = outMetrics.density;

    int adWidth = (int) (widthPixels / density);

    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);

}

public int getScreenWidth() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
        Insets insets = windowMetrics.getWindowInsets()
                .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
        return windowMetrics.getBounds().width() - insets.left - insets.right;
    } else {
        DisplayMetrics outMetrics = getResources().getDisplayMetrics();
        return outMetrics.widthPixels;
    }
}
0
votes

Update Admob and run it.

Admob release notes say, Admob did not support Android 11. From version 19.4.0, they support Android 11.

https://developers.google.com/admob/android/rel-notes