Search code examples
androidads

how to call MobileAds.initialize properly in android?


I am using a listener on MobileAds.initialize so that it calls Interstitial_Show and Interstitial_SHOW in onInitializationComplete

            MobileAds.initialize(context, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                Log.d("update_statut","onInitializationComplete"+initializationStatus);
                mCallbackTwo.onClickTwo("Interstitial_LOAD");
                mCallback.onClick("Interstitial_SHOW");
            }
        });

Splash Activity

@Override
public void onClickTwo(String value) { Interstitial_LOAD();  }


@Override
public void onClick(String value ) {
if (!isFinishing()){
    Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {
    if( (!jsonfetch.check_version) && jsonfetch.isupdated){
        update_dialog();
        Log.d("update_statut", "Update dialog ");
    } else {
        Log.d("update_statut", "Interstitial_SHOW");
        Interstitial_SHOW();
    }
        } 
         }, 5000);
} else  Log.d("update_statut", "isFinishing : TRUE");
}


    private void Interstitial_LOAD()  {
        AdRequest adRequestI = new AdRequest.Builder().build();
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId(inter_code);
        interstitial.loadAd(adRequestI);
        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                Log.d("update_statut", "onAdClosed , Start first_java Activity");
                startActivity(new Intent(splash.this, first_java.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
                splash.this.finish();
            }
        });
    }

private void Interstitial_SHOW() {
        if (interstitial.isLoaded()) {
            interstitial.show();
            return;
        }
        Log.d("update_statut", "interstitial is not loaded in splash , Start first_java Activity");
        startActivity(new Intent(splash.this, first_java.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
        splash.this.finish();
}

I would like to know if this is a safe way ( to put the startActivity code in the Initialization Listener )

What are the cases when the Initialization can't be completed ? ( what if the admob has been suspended for example )

Is there any Listener triggers when Initialization is not completed ?


Solution

  • From the documentation , calling initialize will :

    Initializes the Google Mobile Ads SDK.

    My guess is that it will setup the SDK with your publisher id, other realative static information and your ad settings to make your first request faster..

    However, you can actually forget to initialize your SDK and your requests will still work because these information will fetched when you make your first request according to the documentation:

    If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.

    So I would say switch your focus to your ad requests by implementing an AdListener . In case you are not served an ad it will inform you that and the reason for that which might be as you said (suspended account) or another reason.

    This is how to implement your AdListener just in case:

    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
        }
    
        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
        }
    
        @Override
        public void onAdOpened() {
            // Code to be executed when an ad opens an overlay that
            // covers the screen.
        }
    
        @Override
        public void onAdClicked() {
            // Code to be executed when the user clicks on an ad.
        }
    
        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }
    
        @Override
        public void onAdClosed() {
            // Code to be executed when the user is about to return
            // to the app after tapping on an ad.
        }
    });