Search code examples
javaandroidadmobadsinterstitial

Control the appearance of an interstitial ad


I have a problem with an interstitial so that it does not appear in the middle of the game but it appears when the player leaves and re-entered Is there a solution to show the middle of the game.

private ZenGL zengl;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
private static final String IAD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
protected AdView adView;
private InterstitialAd mInterstitialAd;
private boolean needAds = false;

public void onAdLoaded() {
    mInterstitialAd.show();
};

@Override
public void onResume() {
    if ( zengl != null )
        zengl.onResume();

    if (adView != null)
        adView.pause();

    if (mInterstitialAd.isLoaded() & (needAds==true)) {
        mInterstitialAd.show();
        needAds=false;
    }  
    super.onResume();
}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
    //  .addTestDevice("")
    .build();

    mInterstitialAd.loadAd(adRequest);
    needAds = true;
}

How do I edit these codes to show a midterm game ad?


Solution

  • private void showAd(){
        new AsyncTask<Void, Void, Void>(){
    
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
            }
        }.execute();
    }
    

    this function will show ad after 3 second ;)