Reputation: 35
I want to use Facebook Audience Network for my Android app, I have integrated interstitialAd in-app and it works fine but it appears as Activity launch. I want to show ad after 30 seconds. this is my code
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(Ad ad) {
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// Show the ad
interstitialAd.show();
}
});
interstitialAd.loadAd();
and I have tried this to schedule my ad but it did not work.
private void showAdWithDelay() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Check if interstitialAd has been loaded successfully
if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
return;
}
// Check if the ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
if(interstitialAd.isAdInvalidated()) {
return;
}
// Show the ad
interstitialAd.show();
}
}, 3000);
}
Upvotes: 0
Views: 1575
Reputation: 17
I think your implementation is breaking AdMob policies and could get you some trouble.
Check this page for help on right/wrong Interstitial implementations (Specially the "Interstitials that unexpectedly launch" point): https://support.google.com/admob/answer/6201362?hl=en&ref_topic=2936214
To comply with AdMob policies I suggest you only show Interstitials in activity/app natural transitions, different implementations could lead you to get banned from Admob.
To do so, you could change your code to first just load the Ad and keep track of its loaded status like this:
boolean isAdLoaded = false;
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener()
{
@Override
public void onAdLoaded(Ad ad)
{
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// The ad has been loaded
isAdLoaded = true;
}
});
interstitialAd.loadAd();
And then show it in a natural transition of the app, for example when the user taps a button to go to a different activity:
shareButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Show interstitial if it is loaded
if(isAdLoaded)
interstitialAd.show();
//Open Activity
startActivity(new Intent(this,OtherActivity.class));
}
});
Upvotes: 1
Reputation: 36
I prefer to do this:
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(Ad ad) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
interstitialAd.show();
}
}, YOUR_TIME);
}
});
interstitialAd.loadAd();
And if you want to be more precise, do it:
final long lastTime = System.currentTimeMillis();
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(Ad ad) {
long now = System.currentTimeMillis();
long timeWait;
if (now - lastTime >= YOUR_TIME){
timeWait = 0;
}else {
timeWait = YOUR_TIME-(now-lastTime);
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
interstitialAd.show();
}
}, timeWait);
}
});
interstitialAd.loadAd();
Upvotes: 1