Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. package com.fochmobile.inc.adsmanager;
  2.  
  3. import android.content.Context;
  4. import android.util.Log;
  5. import android.view.View;
  6.  
  7. import com.google.android.gms.ads.AdListener;
  8. import com.google.android.gms.ads.AdRequest;
  9. import com.google.android.gms.ads.AdView;
  10. import com.google.android.gms.ads.InterstitialAd;
  11. import com.google.android.gms.ads.NativeExpressAdView;
  12.  
  13. /**
  14. * ADS MANAGER Class: An open source Java class.
  15. * @Author ESSARE AYOUB
  16. * @Created on January 2017
  17. * @Company FochMobile Inc (c) By ESSARE AYOUB
  18. * @Copyright (c) 2017 | All RIGHTS RESERVED :v
  19. */
  20.  
  21. public class AdsManager{
  22.  
  23. // Your Admob Interstitial ID here !!
  24. private static final String ADMOB_INTERSTITIAL_ID = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxx";
  25. private Context mContext;
  26. private InterstitialAd interstitialAd;
  27.  
  28. /**
  29. * Main Constructor for initializing class
  30. * It takes only one parameter 'Context'
  31. * @param context
  32. * Context is for accessing app resources such
  33. * as strings.xml etc...
  34. */
  35. public AdsManager(Context context){
  36. mContext = context;
  37. }
  38.  
  39. /**
  40. * This Method loads ads in AdView
  41. * The AdView can be passed from any
  42. * Class or Activity as parameter
  43. * @param adView
  44. */
  45. public void LoadAdsBannerWithInterstitial(final AdView adView){
  46. if(adView != null) {
  47. AdRequest adRequest = new AdRequest.Builder().build();
  48. adView.loadAd(adRequest);
  49. }
  50. // AdView will be hidden from the activity until it loads.
  51. adView.setVisibility(View.INVISIBLE);
  52. adView.setAdListener(new AdListener() {
  53. @Override
  54. public void onAdClosed() {
  55. super.onAdClosed();
  56. Log.d("==> Banner Ad:", " Closed by user!");
  57. }
  58.  
  59. @Override
  60. public void onAdFailedToLoad(int i) {
  61. super.onAdFailedToLoad(i);
  62. Log.d("==> Banner Ad:", " Failed to load!");
  63. }
  64.  
  65. @Override
  66. public void onAdOpened() {
  67. super.onAdOpened();
  68. Log.d("==> Banner Ad:", " Clicked by user!");
  69. }
  70.  
  71. @Override
  72. public void onAdLoaded() {
  73. super.onAdLoaded();
  74. Log.d("==> Banner Ad:", " Loaded successfully!");
  75. adView.setVisibility(View.VISIBLE);
  76. }
  77.  
  78. });
  79. // Just load INTERSTITIAL Ads
  80. LoadInterstitial();
  81.  
  82. }
  83.  
  84. /**
  85. * This Method loads ads in NativeExpressAdView
  86. * The NativeExpressAdView can be passed from any
  87. * Class or Activity as parameter
  88. * @param nativeExpressAdView
  89. */
  90. public void LoadAdsNativeWithInterstitial(final NativeExpressAdView nativeExpressAdView){
  91. if(nativeExpressAdView != null){
  92. nativeExpressAdView.loadAd(new AdRequest.Builder().build());
  93. }
  94. nativeExpressAdView.setVisibility(View.GONE);
  95. nativeExpressAdView.setAdListener(new AdListener() {
  96. @Override
  97. public void onAdClosed() {
  98. super.onAdClosed();
  99. }
  100.  
  101. @Override
  102. public void onAdFailedToLoad(int i) {
  103. super.onAdFailedToLoad(i);
  104. }
  105.  
  106. @Override
  107. public void onAdOpened() {
  108. super.onAdOpened();
  109. }
  110.  
  111. @Override
  112. public void onAdLoaded() {
  113. super.onAdLoaded();
  114. nativeExpressAdView.setVisibility(View.VISIBLE);
  115. }
  116. });
  117.  
  118. LoadInterstitial();
  119.  
  120.  
  121. }
  122.  
  123. /**
  124. * This Method loads interstitial ads
  125. * but keep it on memory until ShowInterstitial()
  126. * is called then the Interstitial ads will popup in full screen mode
  127. */
  128. public void LoadInterstitial(){
  129. interstitialAd = new InterstitialAd(mContext);
  130. interstitialAd.setAdUnitId(ADMOB_INTERSTITIAL_ID);
  131. interstitialAd.loadAd(new AdRequest.Builder().build());
  132. interstitialAd.setAdListener(new AdListener() {
  133. @Override
  134. public void onAdClosed() {
  135. super.onAdClosed();
  136. Log.d("==> Interstitial Ad:", " Closed by user");
  137. }
  138.  
  139. @Override
  140. public void onAdFailedToLoad(int i) {
  141. super.onAdFailedToLoad(i);
  142. Log.d("==> Interstitial Ad:", " Failed to load");
  143. }
  144.  
  145. @Override
  146. public void onAdOpened() {
  147. super.onAdOpened();
  148. Log.d("==> Interstitial Ad:", " Opened by user");
  149. }
  150.  
  151. @Override
  152. public void onAdLoaded() {
  153. super.onAdLoaded();
  154. Log.d("==> Interstitial Ad:", " Loaded successfully");
  155. }
  156. });
  157. }
  158.  
  159. /**
  160. * This method shows the interstitial ads on the screen in fullScreen
  161. * NOTICE: Please call LoadInterstitial() method first or ShowInterstitial()
  162. * would be null
  163. */
  164. public void ShowInterstitial(){
  165. if(interstitialAd != null && interstitialAd.isLoaded()){
  166. interstitialAd.show();
  167. }
  168. // Reload interstitial after ShowInterstitial is called.
  169. LoadInterstitial();
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement