Created
January 10, 2021 07:38
-
-
Save SurajBahadur/b2f2b6d4561e19393245e37d32e21e8a to your computer and use it in GitHub Desktop.
Sample Demo To Show App Open Ads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication.utils | |
import android.app.Activity | |
import android.app.Application.ActivityLifecycleCallbacks | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.OnLifecycleEvent | |
import androidx.lifecycle.ProcessLifecycleOwner | |
import com.example.myapplication.FontApplication | |
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 com.google.android.gms.ads.appopen.AppOpenAd.AppOpenAdLoadCallback | |
import java.util.* | |
class AppOpenManager(private val myApplication: FontApplication) : ActivityLifecycleCallbacks, LifecycleObserver { | |
private var appOpenAd: AppOpenAd? = null | |
private var loadCallback: AppOpenAdLoadCallback? = null | |
private var currentActivity: Activity? = null | |
//ad expiration | |
private var loadTime: Long = 0 | |
companion object { | |
private const val LOG_TAG = "AppOpenManager" | |
private const val AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294" | |
private var isShowingAd = false | |
} | |
/** | |
* Constructor | |
*/ | |
init { | |
myApplication.registerActivityLifecycleCallbacks(this) | |
ProcessLifecycleOwner.get().lifecycle.addObserver(this) | |
} | |
/** | |
* Request an ad | |
*/ | |
fun fetchAd() { | |
Log.d(LOG_TAG, currentActivity!!.localClassName) | |
// Have unused ad, no need to fetch another. | |
if (isAdAvailable) { | |
return | |
} | |
loadCallback = object : AppOpenAdLoadCallback() { | |
/** | |
* Called when an app open ad has loaded. | |
* | |
* @param ad the loaded app open ad. | |
*/ | |
override fun onAppOpenAdLoaded(ad: AppOpenAd) { | |
appOpenAd = ad | |
loadTime = Date().time | |
} | |
/** | |
* Called when an app open ad has failed to load. | |
* | |
* @param loadAdError the error. | |
*/ | |
override fun onAppOpenAdFailedToLoad(loadAdError: LoadAdError) { | |
// Handle the error. | |
} | |
} | |
val request = adRequest | |
AppOpenAd.load(myApplication, AD_UNIT_ID, request, AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback) | |
} | |
/** Shows the ad if one isn't already showing. */ | |
fun 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.") | |
val fullScreenContentCallback: FullScreenContentCallback = object : FullScreenContentCallback() { | |
override fun onAdDismissedFullScreenContent() { | |
// Set the reference to null so isAdAvailable() returns false. | |
appOpenAd = null | |
isShowingAd = false | |
fetchAd() | |
} | |
override fun onAdFailedToShowFullScreenContent(adError: AdError) {} | |
override fun onAdShowedFullScreenContent() { | |
isShowingAd = true | |
} | |
} | |
appOpenAd!!.show(currentActivity, fullScreenContentCallback) | |
} else { | |
Log.d(LOG_TAG, "Can not show ad.") | |
fetchAd() | |
} | |
} | |
/** | |
* Creates and returns ad request. | |
*/ | |
private val adRequest: AdRequest | |
private get() = AdRequest.Builder().build() | |
/** Utility method to check if ad was loaded more than n hours ago. */ | |
private fun wasLoadTimeLessThanNHoursAgo(numHours: Long): Boolean { | |
val dateDifference = Date().time - loadTime | |
val numMilliSecondsPerHour: Long = 3600000 | |
return dateDifference < numMilliSecondsPerHour * numHours | |
} | |
/** | |
* Utility method that checks if ad exists and can be shown. | |
*/ | |
val isAdAvailable: Boolean | |
get() = appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4) | |
/** ActivityLifecycleCallback methods */ | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {} | |
override fun onActivityStarted(activity: Activity) { | |
currentActivity = activity | |
} | |
override fun onActivityResumed(activity: Activity) { | |
currentActivity = activity | |
} | |
override fun onActivityPaused(activity: Activity) {} | |
override fun onActivityStopped(activity: Activity) {} | |
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} | |
override fun onActivityDestroyed(activity: Activity) { | |
currentActivity = null | |
} | |
/** LifecycleObserver methods */ | |
@OnLifecycleEvent(Lifecycle.Event.ON_START) | |
fun onStart() { | |
showAdIfAvailable() | |
Log.d(LOG_TAG, "onStart") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FontApplication : Application() { | |
lateinit var appOpenManager: AppOpenManager | |
override fun onCreate() { | |
super.onCreate() | |
MobileAds.initialize(this) { } | |
appOpenManager = AppOpenManager(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment