I want my Android application to display ad – InterstitialAd
. I want it appears after the welcome screen. It means that I have WelcomeActicity that appears on app start, and recently after this – the ad is displayed.
I create InterstitialAd
and start loading ad in application’s OnCreate()
method. If application was closed for the long time, ad loads for more then 13 seconds. Of course this is inappropriate – user should not wait for so long. I made this tests building release apk that was directly installed on my phone Xiaomi Redmi 4X.
So the question is – should it be done in some other way? Or ad is always loaded for the long time and I just need to move it forward in user’s workflow?
In app's OnCreate()
I start loading
public class MyApp extends Application {
private FullScreenAd mFullScreenAd;
@Override
public void onCreate() {
super.onCreate();
mFullScreenAd = new FullScreenAd(this);
mFullScreenAd.loadAd();
}
}
Implementation is the following:
public class FullScreenAd{
private static final String APP_AD_ID = "ca-app-pub-3940256099942544~3347511713";
private static final String SCREEN_AD_ID = "ca-app-pub-3940256099942544/1033173712";
private String mAdAppId;
private String mAdScreenId;
private InterstitialAd mInterstitialAd;
private long mLoadStart;
public FullScreenAd(Context context) {
super();
DebugLogger.d();
mContext = context;
initAdIDs(null, null);
}
private void initAdIDs(String adAppId, String adScreenId) {
DebugLogger.d();
mAdAppId = (adAppId != null) ? adAppId : APP_AD_ID;
mAdScreenId = (adScreenId != null) ? adScreenId : SCREEN_AD_ID;
mInterstitialAd = new InterstitialAd(mContext);
mInterstitialAd.setAdUnitId(mAdScreenId);
setInterstitialAdListener();
}
public void loadAd() {
// Initialize the Mobile Ads SDK.
MobileAds.initialize(mContext, mAdAppId);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Start loading the ad in the background.
mAdIsLoaded.set(false);
mInterstitialAd.loadAd(adRequest);
mLoadStart = System.currentTimeMillis();
}
private void setInterstitialAdListener() {
DebugLogger.d();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
DebugLogger.d();
DebugLogger.d(String.format("Ad loaded in %d ms", System.currentTimeMillis() - mLoadStart));
mAdIsLoaded.set(true);
}
});
}
public void showAd(OnAdClosedAction action) {
DebugLogger.d();
mInterstitialAd.show();
}
}
Loading an AddMob
interstitial ad takes some time. Usually it's as quick as a few seconds, but can go up to 20-30 seconds if the connection is poor, or the ad contains animation/video. It might even not load at all!
A good practice is to load and keep in memory the ad upon app launch, and show it later at some point - make it all as seamless as possible. Make sure that ad loading/displaying is not interfering with user experience - don't make the user wait for your ad loading.