android接入admob开屏广告

android接入admob开屏广告,第1张

概述最近admob推出开屏广告、刚好公司也在做海外广告这块的业务。于是开始接入开屏广告,展示图如下:个人感觉开屏广告比较像插屏广告、实现如下:1、添加依赖;implementation'com.google.firebase:firebase-ads:19.4.0'implementation"androidx.lifecycle:lifecycle-extensions:$l

最近admob推出开屏广告、刚好公司也在做海外广告这块的业务。于是开始接入开屏广告,展示图如下:

个人感觉开屏广告比较像插屏广告、实现如下:

1、添加依赖;

 implementation 'com.Google.firebase:firebase-ads:19.4.0'implementation "androIDx.lifecycle:lifecycle-extensions:$lifecycle_version"    implementation "androIDx.lifecycle:lifecycle-runtime:$lifecycle_version"    annotationProcessor "androIDx.lifecycle:lifecycle-compiler:$lifecycle_version"

 2、AndroIDManifest.xml

<Meta-data    androID:name="com.Google.androID.gms.ads.APPliCATION_ID"    androID:value="ca-app-pub-3940256099942544~3347511713" />

3、关于admob测试广告位:

  String test_appID = "ca-app-pub-3940256099942544~3347511713";    String test_interID = "ca-app-pub-3940256099942544/1033173712";    String test_banner = "ca-app-pub-3940256099942544/6300978111";    String test_native = "ca-app-pub-3940256099942544/2247696110";    String test_open = "ca-app-pub-3940256099942544/3419835294";

4、Application添加如下代码:

 private static AppOpenManager appOpenManager;  @OverrIDe    public voID onCreate() {        super.onCreate();      initopenScreen();    } private voID initopenScreen() {        MobileAds.initialize(                this,                initializationStatus -> {                });        appOpenManager = new AppOpenManager(this);    }

5、AppOpenManager

package com.duonami.funny.flower.ads;/** * 开屏广告工具类 */import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androIDx.lifecycle.lifecycleObserver;import androIDx.lifecycle.OnlifecycleEvent;import androIDx.lifecycle.ProcesslifecycleOwner;import com.Google.androID.gms.ads.AdError;import com.Google.androID.gms.ads.AdRequest;import com.Google.androID.gms.ads.FullScreenContentCallback;import com.Google.androID.gms.ads.LoadAdError;import com.Google.androID.gms.ads.appopen.AppOpenAd;import java.util.Date;import static androIDx.lifecycle.lifecycle.Event.ON_START;/** * Prefetches App Open Ads and handles lifecycle detection. */public class AppOpenManager implements lifecycleObserver, FlowerApplication.ActivitylifecycleCallbacks {    private static final String LOG_TAG = "AppOpenManager";    private static final String AD_UNIT_ID = Constant.release_open;    private AppOpenAd appOpenAd = null;    private AppOpenAd.AppOpenAdLoadCallback loadCallback;    private final FlowerApplication myApplication;    private Activity currentActivity;    //展示广告    private static boolean isShowingAd = false;    private long loadTime = 0;    /**     * Constructor     */    public AppOpenManager(FlowerApplication myApplication) {        this.myApplication = myApplication;        //监听activity        this.myApplication.registeractivitylifecycleCallbacks(this);        ProcesslifecycleOwner.get().getlifecycle().addobserver(this);    }    @OnlifecycleEvent(ON_START)    public voID onStart() {        showAdIfAvailable();        Log.d(LOG_TAG, "onStart");    }    /**     * Request an ad     */    public voID fetchAd() {        // We will implement this below.        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) {                AppOpenManager.this.appOpenAd = ad;                AppOpenManager.this.loadTime = (new Date()).getTime();                Log.d(LOG_TAG, "openScreenAd is load");            }            /**             * Called when an app open ad has Failed to load.             *             * @param loadAdError the error.             */            @OverrIDe            public voID onAppOpenAdFailedToload(LoadAdError loadAdError) {                // Handle the error.                Log.d(LOG_TAG, "开屏加载失败--" + loadAdError.getMessage());            }        };        AdRequest request = getAdRequest();        AppOpenAd.load(                myApplication, AD_UNIT_ID, request,                AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);    }    /**     * 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);    }    @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() {        // 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();        }    }    /**     * Utility method to check if ad was loaded more than n hours ago.     */    private boolean wasLoadTimeLessthanNHoursAgo(long numHours) {        long dateDifference = (new Date()).getTime() - this.loadTime;        long numMilliSecondsPerHour = 3600000;        return (dateDifference < (numMilliSecondsPerHour * numHours));    }}

整个接入就完成了,运行项目即可展示开屏

 

总结

以上是内存溢出为你收集整理的android接入admob开屏广告全部内容,希望文章能够帮你解决android接入admob开屏广告所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存