Showing Interstitial Ads With Timer in Splash Screen

30-10-2019

Showing admob interstitial ads when splash screen is loaded with a timer can be implemented as follows:

package com.codesenior.admob;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;
    private Timer waitTimer;
    private boolean interstitialCanceled;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Make sure this is before calling super.onCreate
        setTheme(R.style.SplashScreenTheme);
        super.onCreate(savedInstanceState);
        initTimer();
        initInterstitialAd();//todo enable in production
    }
    
    private void initTimer() {
        waitTimer = new Timer();
        waitTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                SplashActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startMainActivity();
                    }
                });
            }
            }, 5000);
        }
        private void startMainActivity() {
            waitTimer.cancel();
            interstitialCanceled=true;
            Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(mainIntent);
            finish();
        }
        private void initInterstitialAd() {
            
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getResources().getString(R.string.ad_interstitial));
            AdRequest adRequestInter = new AdRequest.Builder().build();
            mInterstitialAd.loadAd(adRequestInter); //todo enable in production
            
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    waitTimer.cancel();
                    if (!interstitialCanceled){
                        mInterstitialAd.show();
                        interstitialCanceled=true;
                    }
                }
                
                public void onAdClicked() {
                    waitTimer.cancel();
                    interstitialCanceled=true;
                }
                
                public void onAdFailedToLoad(int var1) {
                    startMainActivity();
                }
            });
        }
        @Override
        public void onResume() {
            super.onResume();
            if (interstitialCanceled) {
                startMainActivity();
            }
        }
        
    }

SplashScreenTheme implementation:

<style name="SplashScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/splash</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDisablePreview">true</item>
</style>

Android Manifest Definition:

<activity
    android:name="com.codesenior.admob.SplashActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

© 2019 All rights reserved. Codesenior.COM