Reputation: 361
I have an activity to show a full-screen image. There is a button to download the image. Before downloading, I need to show an interstitial ad. After download, I need to request & load a new interstitial ad. I have coded the same as following:
public class FullScreenViewActivity extends Activity implements OnClickListener {
private InterstitialAd interstitial;
private AdRequest adRequest;
....
@Override
protected void onCreate(Bundle savedInstanceState) {
interstitial = new InterstitialAd(FullScreenViewActivity.this);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id)); //live ad unit
...
}
protected void onResume() {
super.onResume();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdClosed() {
downloadImage();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
}
public void onAdFailedToLoad(int var1) {
downloadImage();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
}
public void onAdLeftApplication() {
downloadImage();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
}
});
}
public AdRequest getInterstitialAdRequest() {
return new AdRequest.Builder().build();
}
}
My problem is that I keep on seeing the same ad every time I click the download button. Is there some problem in my logic? Or is it that admob is giving me same ad everytime I request?
Upvotes: 0
Views: 1509
Reputation: 416
best way to use it is:
void requestInterstitial(){
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
Upvotes: 0
Reputation: 5351
I don't see anything in your code that would cause you to get the same ad repeatedly. It's possible that wherever you are in the world, there is one particular advertiser buying up almost all of the demand, which can cause a single ad to appear repeatedly. Usually it's a temporary issue, and will go away with time.
I would recommend taking the loadAd
calls out of these methods, though:
public void onAdFailedToLoad(int var1) {
downloadImage();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
}
public void onAdLeftApplication() {
downloadImage();
adRequest = getInterstitialAdRequest();
interstitial.loadAd(adRequest);
}
onAdLeftApplication
is called when the user taps on an ad and a browser (or another app) is opened in response. When they return, onAdClosed
will still be called, so as your code stands, you'll end up with two calls to loadAd
. I'd remove the one in onAdLeftApplication
and leave the one in onAdClosed
.
Calling loadAd
a second time to recover from a failed ad in onAdFailedToLoad
is a good idea, but you don't appear to have any circuit-breaking logic or increase in the time before trying again in the case of repeated failures. This could cause an infinite loop if the user's network connection goes down (Ad #1 fails immediately, which causes Ad #2 to try and load, which fails immediately and causes Ad #3...). Try using a counter to cut off the attempts after a certain number, or use a Handler
to introduce a gradually increasing delay.
Also, one final note: If you're testing your app, you should use test ads!
Upvotes: 1