Ads SMART_BANNER aren't always loaded in landscape orientation

不想你离开。 提交于 2019-12-24 14:07:42

问题


It works ok for portrait orientation 100%, onAdLoaded is always called and Ad appears

But in landscape orientation they are loaded very rarely, onAdFailedToLoad is usually called

I mean what the point of using SMART_BANNER if it's loaded very rarely in landscape mode?

My code (I always re-create AdView and request new ad when orientation changes)

Manifest

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"

XML Activity Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/adViewWrapper"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
    </FrameLayout>
    ...

Java

private void setAd() { // I call it in onCreate
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("...............");
    mAdView.setId(R.id.adView);
    FrameLayout.LayoutParams lp = new FrameLayout
            .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    mAdView.setLayoutParams(lp);
    ((FrameLayout) findViewById(R.id.adViewWrapper)).addView(mAdView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.setAdListener(mBannerAdListener);
    mAdView.loadAd(adRequest);
}

@Override
public void onConfigurationChanged(final Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    Log.i(TAG, "onConfigurationChanged");
    if (mCurrentOrientation != newConfig.orientation) {
        mCurrentOrientation = newConfig.orientation;
        if (mAdView != null) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mAdView.getLayoutParams();
            final FrameLayout parentLayout = (FrameLayout) mAdView.getParent();
            parentLayout.setVisibility(View.GONE);
            int adViewId = mAdView.getId();
            String adUnitId = mAdView.getAdUnitId();
            AdSize adSize = mAdView.getAdSize();
            mAdView.destroy();
            parentLayout.removeView(mAdView);

            mAdView = new AdView(this);
            mAdView.setAdSize(adSize);
            mAdView.setAdUnitId(adUnitId);
            mAdView.setId(adViewId);
            mAdView.setLayoutParams(lp);
            parentLayout.addView(mAdView);

            mAdView.setAdListener(mBannerAdListener);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }
    }
}

private AdListener mBannerAdListener = new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
        Log.i(TAG, "AdListener onAdLoaded");
        findViewById(R.id.adViewWrapper).setVisibility(View.VISIBLE);
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        Log.i(TAG, "AdListener onAdFailedToLoad");
        // Code to be executed when an ad request fails.
        findViewById(R.id.adViewWrapper).setVisibility(View.GONE);
    }

    @Override
    public void onAdOpened() {
        Log.i(TAG, "AdListener onAdOpened");
        // Code to be executed when an ad opens an overlay that
        // covers the screen.
    }

    @Override
    public void onAdLeftApplication() {
        Log.i(TAG, "AdListener onAdLeftApplication");
        // Code to be executed when the user has left the app.
    }

    @Override
    public void onAdClosed() {
        Log.i(TAG, "AdListener onAdClosed");
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.
    }
};

来源:https://stackoverflow.com/questions/51349918/ads-smart-banner-arent-always-loaded-in-landscape-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!