android 如何将“OpenAds”Admob与Splash相结合?

piztneat  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(164)

我有一个“开放广告”类型的admob广告。
这在应用程序启动时起作用,不幸的是,应用程序经常被拒绝,因为广告在应用程序的内容之前被看到...
所以我把Splash_Activity放上去稍微露一点,广告就出现了,方法是有效的,申请被接受了。

**问题:**当我为Splash_Activity设置的时间到期时,广告会随之消失,而无需为应用程序按继续,在这种情况下,广告会保留在应用程序的后台。
**必需:**广告出现时,Splash_Activity屏幕停止,除非您关闭广告,否则不会消失。

应用程序打开管理器:

public class AppOpenManager implements LifecycleObserver, Application.ActivityLifecycleCallbacks {

    private static final String LOG_TAG = "AppOpenManager";
    private static final String AD_UNIT_ID = "ca-app-pub-****/****";
    private AppOpenAd appOpenAd = null;
    private long loadTime = 0;

    private AppOpenAd.AppOpenAdLoadCallback loadCallback;
    private Activity currentActivity;
    private static boolean isShowingAd = false;

    private final GlobalVar Splash_Activity;

    /**
     * Constructor
     */
    public AppOpenManager(GlobalVar splash_Activity) {
        this.Splash_Activity = splash_Activity;
        this.Splash_Activity.registerActivityLifecycleCallbacks(this);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    /** LifecycleObserver methods */
    @OnLifecycleEvent(ON_START)
    public void onStart() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showAdIfAvailable();

            }
        }, 1500);

        Log.d(LOG_TAG, "onStart");
    }

    /** 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.setFullScreenContentCallback(fullScreenContentCallback);
            appOpenAd.show(currentActivity);

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

    /**
     * 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 onAdLoaded(AppOpenAd ad) {
                        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 onAdFailedToLoad(LoadAdError loadAdError) {
                        // Handle the error.
                    }

                };
        AdRequest request = getAdRequest();
        AppOpenAd.load(
                Splash_Activity, AD_UNIT_ID, request,
                AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
    }
    // We will implement this below.

    /**
     * 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;
    }
    /** 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));
    }
}

飞溅_活动

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (go) {
                startActivity(new Intent(Splash_Activity.this, home_main.class));
                finish();
            }
        }
    }, 4200);
}
elcex8rz

elcex8rz1#

首先,您需要创建一个回调侦听器来了解广告何时被取消,并在取消之前停止进一步的代码执行。

public interface AdListener {
        void onCompleted();
    }

使用listener参数更改showAdIfAvailable的代码,如下所示。

public void showAdIfAvailable(@NonNull AdListener listener) {
        // 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.");
            appOpenAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                @Override
                public void onAdDismissedFullScreenContent() {
                    // Set the reference to null so isAdAvailable() 
                    this.appOpenAd = null;
                    isShowingAd = false;
                    fetchAd();

                    // Inform caller that we have done
                    listener.onCompleted();
                }

                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    appOpenAd = null;
                    // Inform caller that we have done
                    listener.onCompleted();
                }

                @Override
                public void onAdShowedFullScreenContent() {
                    isShowingAd = true;
                }
            });
            appOpenAd.show(currentActivity);
        } else {
            Log.d(LOG_TAG, "Can not show ad.");

            // Inform caller that we have done
            listener.onCompleted();

            // Load new ad
            fetchAd();
        }
    }

调用以显示应用程序onen广告,如下所示:

showAdIfAvailable(SplashActivity.this, new AdListener() {
            @Override
            public void onCompleted() {
                
                // Start your activity from here
                
            }
        });

但是,这只是一个过程的演示,您可以根据需要对其进行更改。
希望能有所帮助。

相关问题