Admob 开屏广告 AppOpenAd

在八月份的时候,admob上架了开屏广告,这个广告样式在国内一些sdk已经是常见了,但对于我们一些个人开发者,没有营业执照是没办法获取到权限。所以只能选择admob。开屏广告目前有限范围开放 beta 测试。而且在官方文档也没显示.

image.png

所以需要我们在admob的后台中去添加这个广告单元才能查看到。

image.png

这样子的话就会看到有广告单元可以选择了,当我们创建完开屏广告单元后就可以看到指南了 https://developers.google.com/admob/android/app-open-ads?hl=zh-CN

下面是admob官方的演示图


yPbX8KY7SealRNQ.gif

具体实现如下,首先创建一个AppOpenManager

public class AppOpenManager implements LifecycleObserver,Application.ActivityLifecycleCallbacks {

    private static final String LOG_TAG = "AppOpenManager";
    private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
    private AppOpenAd appOpenAd = null;
    private Activity currentActivity;
    private AppOpenAd.AppOpenAdLoadCallback loadCallback;
    private static boolean isShowingAd = false;
    private final MyApplication myApplication;
    private long loadTime = 0;
    private static final String TAG = "AppOpenManager";

    /** Constructor
     * */
    public AppOpenManager(MyApplication myApplication) {
        this.myApplication = myApplication;
        this.myApplication.registerActivityLifecycleCallbacks(this);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    /** Creates and returns ad request.
     * 创建并返回广告请求。
     * */
    private AdRequest getAdRequest() {
        return new AdRequest.Builder().build();
    }

    /** Utility method that checks if ad exists and can be shown.
     * 检查广告是否存在并可以显示的实用程序方法。
     * */
    public boolean isAdAvailable() {
        return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
    }

    /** Utility method to check if ad was loaded more than n hours ago.
     * 检查广告是否已加载n小时前的实用方法。
     * 目前使用的是4小时
     * */
    private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
        long dateDifference = (new Date()).getTime() - this.loadTime;
        long numMilliSecondsPerHour = 3600000;
        return (dateDifference < (numMilliSecondsPerHour * numHours));
    }

    /** LifecycleObserver methods
     *LifecycleObserver方法
     * */
    @OnLifecycleEvent(ON_START)
    public void onStart() {
        showAdIfAvailable();
    }

    /** Request an ad
     *请求广告
     */
    public void fetchAd() {
        // Have unused ad, no need to fetch another.
        if (isAdAvailable()) {
            return;
        }

        loadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
                    /**
                     * Called when an app open ad has loaded.
                     *
                     * @param ad the loaded app open ad.
                     */
                    @Override
                    public void onAppOpenAdLoaded(AppOpenAd ad) {
                        Log.e(TAG, "onAppOpenAdLoaded: " );
                        AppOpenManager.this.appOpenAd = ad;
                        AppOpenManager.this.loadTime = (new Date()).getTime();
                    }

                    /**
                     * Called when an app open ad has failed to load.
                     *
                     * @param loadAdError the error.
                     */
                    @Override
                    public void onAppOpenAdFailedToLoad(LoadAdError loadAdError) {
                        Log.e(TAG, "onAppOpenAdFailedToLoad: "+loadAdError.toString() );
                        // Handle the error.
                    }

                };
        AdRequest request = getAdRequest();
        AppOpenAd.load(
                myApplication, AD_UNIT_ID, request,
                AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
    }

    /** ActivityLifecycleCallback methods
     * registerActivityLifecycleCallbacks 可用于监听所有 Activity 事件。通过监听 Activity 启动和销毁的时间,您可以跟踪对当前 Activity 的引用,然后根据该 Activity 展示您的开屏广告。
     * */
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

    @Override
    public void onActivityStarted(Activity activity) {
        currentActivity = activity;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        currentActivity = activity;
    }

    @Override
    public void onActivityStopped(Activity activity) {}

    @Override
    public void onActivityPaused(Activity activity) {}

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}

    @Override
    public void onActivityDestroyed(Activity activity) {
        currentActivity = null;
    }

    /** Shows the ad if one isn't already showing.
     * 如果尚未展示,则显示广告。
     * */
    public void showAdIfAvailable() {
        Log.e(TAG, "showAdIfAvailable: " );
        // Only show ad if there is not already an app open ad currently showing
        // and an ad is available.
        if (!isShowingAd && isAdAvailable()) {
            Log.d(LOG_TAG, "Will show ad.");
            FullScreenContentCallback fullScreenContentCallback =
                    new FullScreenContentCallback() {
                        @Override
                        public void onAdDismissedFullScreenContent() {
                            // Set the reference to null so isAdAvailable() returns false.
                            AppOpenManager.this.appOpenAd = null;
                            isShowingAd = false;
                            fetchAd();
                        }

                        @Override
                        public void onAdFailedToShowFullScreenContent(AdError adError) {

                        }

                        @Override
                        public void onAdShowedFullScreenContent() {
                            isShowingAd = true;
                        }
                    };

            appOpenAd.show(currentActivity, fullScreenContentCallback);

        } else {
            Log.d(LOG_TAG, "Can not show ad.");
            fetchAd();
        }
    }
}

然后去到我们的Application中

public class MyApplication extends Application {

    public static AppOpenManager appOpenManager;

    @Override
    public void onCreate() {
        super.onCreate();
        MobileAds.initialize(
                this,
                new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {}
                });
        appOpenManager = new AppOpenManager(this);
    }
}

这样子就已经完成了。当我们首次打开app时他们请求广告,当app回到手机主界面再返回我们的应用,就可以看到全屏广告了。
其实这个看起来 InterstitialAd并不大,但他并不会占满你整个屏幕。
官网也给出了最佳做法,应该何时展示开屏广告。

最佳做法
借助开屏广告,您可以在用户首次启动应用和切换应用期间通过应用的加载屏幕获利,不过,还请务必考虑一些最佳做法,以便用户喜欢使用您的应用。最佳做法如下所示:
在用户使用几次您的应用后展示第一个开屏广告。
在用户等待您的应用加载时展示开屏广告。
如果开屏广告位于加载屏幕上,并且加载屏幕在用户关闭广告之前已加载完毕,您可能需要通过 onAdDismissedFullScreenContent() 方法关闭加载屏幕。

作为开发者,更应该结合实际情况来处理。毕竟如果广告做的不好,会让用户很反感。

你可能感兴趣的:(Admob 开屏广告 AppOpenAd)