반응형
최신 글이 추가 되었습니다.
2022.01.14 - [안드로이드] - [안드로이드] 앱 오프닝 광고 구현 Version: Kotlin
오늘은 애드몹에 추가된 앱 오프닝 광고 구현 예시 최신을 적어보고자 합니다.
기존의 적어둔 포스트를 수정하였습니다.
사실 아래 코드는 Google 에서 만든 레퍼런스를 조금 수정하여 저의 애플리케이션에 적용한 코드입니다.
package com.keelim.nandadiagnosis.utils;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
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 com.keelim.nandadiagnosis.BuildConfig;
import com.keelim.nandadiagnosis.MyApplication;
import java.util.Date;
import timber.log.Timber;
/**
* Prefetches App Open Ads.
*/
public class AppOpenManager implements LifecycleObserver, Application.ActivityLifecycleCallbacks {
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294";
private static boolean isShowingAd = false;
private AppOpenAd appOpenAd = null;
private AppOpenAd.AppOpenAdLoadCallback loadCallback;
private final MyApplication myApplication;
private Activity currentActivity;
private long loadTime = 0;
/**
* Constructor
*/
public AppOpenManager(MyApplication myApplication) {
this.myApplication = myApplication;
this.myApplication.registerActivityLifecycleCallbacks(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
showAdIfAvailable();
Timber.d("onStart");
}
/**
* Request an ad
*/
public void fetchAd() {
if (isAdAvailable()) {
return;
}
loadCallback =
new AppOpenAd.AppOpenAdLoadCallback() {
/**
* Called when an app open ad has loaded.
*
* @param ad the loaded app open ad.
*/
/**
* Called when an app open ad has failed to load.
*
* @param loadAdError the error
*/
};
AdRequest request = getAdRequest();
if (BuildConfig.DEBUG)
AppOpenAd.load(myApplication, AD_UNIT_ID, request, AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback); //test unit ID
else
AppOpenAd.load(myApplication, "YOUR UNIT ID", request, AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback); //test unit ID
}
/**
* 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();
}
@Override
public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
currentActivity = null;
}
public void showAdIfAvailable() {
if (!isShowingAd && isAdAvailable()) {
Timber.d( "will show ad");
FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
AppOpenManager.this.appOpenAd = null;
isShowingAd = false;
fetchAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}
@Override
public void onAdShowedFullScreenContent() {
isShowingAd = true;
}
};
appOpenAd.show(currentActivity);
} else {
Timber.d("Can not show ad. ");
fetchAd();
}
}
/**
* Utility method to check if ad was loaded more than n hours ago.
*/
private boolean wasLoadTimeLessThanNHoursAgo() {
long dateDifference = (new Date()).getTime() - this.loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * (long) 4));
}
}
저는 위와 같은 코드를 작성하여 애드몹에서 이번에 추가된 앱 오프닝 광고 구현해보았습니다. 필요하신 분들은 적절하게 수정을 해서 바꾸는 것도 좋을 것 같습니다.
반응형
'안드로이드' 카테고리의 다른 글
[안드로이드] 리싸이클러 뷰 개선기 (코드 특이하게 쓰네 ) (0) | 2021.06.15 |
---|---|
[안드로이드] Android Studio 2021.1.1 Canary Bumblebee gradle error (0) | 2021.05.30 |
[안드로이드] 멀티 모듈 공통 gradle 그리고 ktlint 적용을 해보자 (0) | 2021.04.14 |
[안드로이드] Firebase Distribution 사용하기 (0) | 2021.03.29 |
[안드로이드] Hi! Compose (0) | 2021.03.01 |
댓글