I got stuck showing interstitial ads after an interval. my code displays only once (after 60 seconds). I want to show the interstitial ads in every 60 seconds interval. I know this is not a good idea to implement ads this way but I need this.. my code is below:
package com.ronie.admobads;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.mInterstitialAd.show();
}
}, 60000);
}
});
}
}
@Abhishek, I tried your way but Interstitial Ads doesn't show. I can see only banner ads. Would you please check if my I am wrong anywhere inside the code? Full code is below:
package com.ronie.admobads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
mInterstitialAd.show();
createRequest();
}
}, 1,1, TimeUnit.MINUTES);
}
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
You can schedule your event in this way :
createRequest();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if(mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
}, 1, 1, TimeUnit.MINUTES);
And createRequest
method
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
EDIT
My mistake, I apologize, thread become dead because of java.lang.IllegalStateException
, Need to call on the main UI thread.
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
});
}
}, 1, 1, TimeUnit.MINUTES);