I'm implementing Admob interstitial ads in a button. But it take 3-4 seconds to load the ads, user wrongly click button again and app get crush. How to preload interstitial ads or load instantly when press the button. Here is my code.
img_ply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdUnitId(getResources().getString(R.string.admob_intertestial_id));
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
mInterstitial.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
Intent inttv = new Intent(SingleChannelActivity.this, TvPlay.class);
inttv.putExtra("url", ChannelUrl);
startActivity(inttv);
}
});
}else{
super.onAdLoaded();
}
}
});
}
});
You should put Interstitial load method into onCreate() function of view, not in onClick event of the button ! So, there will be only mInterstitial.show() method in onClick() event. You must load the inters ads onCreate() and inters ads will be ready whenever you need..