广告SMART_BANNER并非始终以横向放置

时间:2018-07-15 15:52:44

标签: android admob google-admob

纵向100%正常,onAdLoaded始终被调用并出现广告

但是在横向放置时,它们很少加载,onAdFailedToLoad通常称为

我的意思是,SMART_BANNER在横向模式下很少加载时有什么意义?

我的代码(方向更改后,我总是重新创建AdView并请求新广告)

清单

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

XML活动布局

<?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.
    }
};

0 个答案:

没有答案