Advertisement
deadman96385

Untitled

Nov 19th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. Create a new class derived from AdView;
  2.  
  3. package com.MyApp;
  4.  
  5. import android.app.Activity; import android.content.Context; import android.util.AttributeSet;
  6.  
  7. import com.google.ads.AdRequest; import com.google.ads.AdSize;
  8.  
  9. public class MyAdView extends com.google.ads.AdView {
  10. public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
  11. super(activity, adSize, adUnitId);
  12. if (MyApp.m_ads_enabled) {
  13. AdRequest adRequest = new AdRequest();
  14. loadAd(adRequest);
  15. }
  16. }
  17.  
  18. public MyAdView(Context context, AttributeSet attrs)
  19. {
  20. super(context, attrs);
  21. if (MyApp.m_ads_enabled) {
  22. AdRequest adRequest = new AdRequest();
  23. loadAd(adRequest);
  24. }
  25. }
  26.  
  27. MyAdView(Context context, AttributeSet attrs, int defStyle)
  28. {
  29. super(context, attrs, defStyle);
  30. if (MyApp.m_ads_enabled) {
  31. AdRequest adRequest = new AdRequest();
  32. loadAd(adRequest);
  33. }
  34. }
  35. in your XML define your advert using MyAdView rather than the regular AdView and set the loadAdOnCreate attribute to false, e.g.;
  36. <com.MyApp.MyAdView android:id="@+id/adView"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. ads:adUnitId="0"
  40. ads:adSize="BANNER"
  41. ads:loadAdOnCreate="false"/>
  42. Then, depending on the value of MyApp.m_ads_enabled when you call setContentView() the ads will either be disabled or enabled.
  43.  
  44. This approach has the advantage that, with ads disabled, no data bandwidth will be used as the ad never gets requested, this may be important to someone on a limited or PAYG data contract.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement