I am integrating AdMob into my app and I wonder how to disable Ads correctly. I want to give the user the ability to disable them. I don\'t want to get any problems with AdM
Create a new class derived from AdView;
package com.MyApp;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
public class MyAdView extends com.google.ads.AdView {
public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
super(activity, adSize, adUnitId);
if (MyApp.m_ads_enabled) {
AdRequest adRequest = new AdRequest();
loadAd(adRequest);
}
}
public MyAdView(Context context, AttributeSet attrs)
{
super(context, attrs);
if (MyApp.m_ads_enabled) {
AdRequest adRequest = new AdRequest();
loadAd(adRequest);
}
}
MyAdView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
if (MyApp.m_ads_enabled) {
AdRequest adRequest = new AdRequest();
loadAd(adRequest);
}
}
in your XML define your advert using MyAdView rather than the regular AdView and set the loadAdOnCreate attribute to false, e.g.;
Then, depending on the value of MyApp.m_ads_enabled when you call setContentView() the ads will either be disabled or enabled.
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.