안드로이드 앱 오프닝 광고 추가하는 방법
안드로이드 앱 오프닝 광고를 추가하는 방법이다.
1. 오프닝 광고 아이디를 애드몹에서 추가해야 한다.
MyApplication 코드
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks, LifecycleObserver { private AppOpenAdManager appOpenAdManager; private Activity currentActivity; @Override public void onCreate() { super.onCreate(); this.registerActivityLifecycleCallbacks(this); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete( @NonNull InitializationStatus initializationStatus) {} }); ProcessLifecycleOwner.get().getLifecycle().addObserver(this); appOpenAdManager = new AppOpenAdManager(); } @OnLifecycleEvent(Lifecycle.Event.ON_START) protected void onMoveToForeground() { appOpenAdManager.showAdIfAvailable(currentActivity); } @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {} @Override public void onActivityStarted(@NonNull Activity activity) { if (!appOpenAdManager.isShowingAd) { currentActivity = activity; } } @Override public void onActivityResumed(@NonNull Activity activity) {} @Override public void onActivityPaused(@NonNull Activity activity) {} @Override public void onActivityStopped(@NonNull Activity activity) {} @Override public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {} @Override public void onActivityDestroyed(@NonNull Activity activity) {} public void showAdIfAvailable( @NonNull Activity activity, @NonNull OnShowAdCompleteListener onShowAdCompleteListener) { appOpenAdManager.showAdIfAvailable(activity, onShowAdCompleteListener); } public interface OnShowAdCompleteListener { void onShowAdComplete(); } private class AppOpenAdManager { private static final String LOG_TAG = "AppOpenAdManager"; private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294"; private AppOpenAd appOpenAd = null; private boolean isLoadingAd = false; private boolean isShowingAd = false; private long loadTime = 0; public AppOpenAdManager() {} private void loadAd(Context context) { if (isLoadingAd || isAdAvailable()) { return; } isLoadingAd = true; AdRequest request = new AdRequest.Builder().build(); AppOpenAd.load(context, AD_UNIT_ID, request, AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, new AppOpenAd.AppOpenAdLoadCallback() { @Override public void onAdLoaded(AppOpenAd ad) { appOpenAd = ad; isLoadingAd = false; loadTime = (new Date()).getTime(); Log.d(LOG_TAG, "onAdLoaded."); } @Override public void onAdFailedToLoad(LoadAdError loadAdError) { isLoadingAd = false; Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.getMessage()); } }); } private boolean wasLoadTimeLessThanNHoursAgo(long numHours) { long dateDifference = (new Date()).getTime() - loadTime; long numMilliSecondsPerHour = 3600000; return (dateDifference < (numMilliSecondsPerHour * numHours)); } private boolean isAdAvailable() { return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4); } private void showAdIfAvailable(@NonNull final Activity activity) { showAdIfAvailable(activity, new OnShowAdCompleteListener() { @Override public void onShowAdComplete() { } }); } private void showAdIfAvailable(@NonNull final Activity activity, @NonNull OnShowAdCompleteListener onShowAdCompleteListener) { if (isShowingAd) { Log.d(LOG_TAG, "The app open ad is already showing."); return; } if (!isAdAvailable()) { Log.d(LOG_TAG, "The app open ad is not ready yet."); onShowAdCompleteListener.onShowAdComplete(); loadAd(activity); return; } Log.d(LOG_TAG, "Will show ad."); appOpenAd.setFullScreenContentCallback(new FullScreenContentCallback() { @Override public void onAdDismissedFullScreenContent() { appOpenAd = null; isShowingAd = false; Log.d(LOG_TAG, "onAdDismissedFullScreenContent."); onShowAdCompleteListener.onShowAdComplete(); loadAd(activity); } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { appOpenAd = null; isShowingAd = false; Log.d(LOG_TAG, "onAdFailedToShowFullScreenContent: " + adError.getMessage()); onShowAdCompleteListener.onShowAdComplete(); loadAd(activity); } @Override public void onAdShowedFullScreenContent() { Log.d(LOG_TAG, "onAdShowedFullScreenContent."); } }); isShowingAd = true; appOpenAd.show(activity); } } }
댓글
댓글 쓰기