Like timing an Interstitial ad?

Asked

Viewed 282 times

3

How to Time an Interstitial Ad?

Example:.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    interstitialAd = new InterstitialAd(Main.this);
    interstitialAd.setAdUnitId(getString(R.string.adMobInter));
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice(getString(R.string.adMob_test))
            .build();
    interstitialAd.loadAd(adRequest);

    interstitialAd.setAdListener(new AdListener() {


        @Override
        public void onAdLoaded() {
            
               displayInterstitial();
            
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            
            onMain();


        }

        @Override
        public void onAdClosed() {
            
            onMain();

        }
    });

}

public void displayInterstitial() {
  
    if (interstitialAd.isLoaded())  {
        interstitialAd.show();

    }
}

public void onMain() {

    setContentView(R.layout.main); }
   

  • What is the programming language? [tag:java] or [tag:c#] for example. Edit in tag if any.

  • Java ( android studio)

  • Francis, you don’t need to enter the language in the title, so you have the tags underneath.

  • Thank you @diegofm

2 answers

1

Fala Francis,

Try to do it that way then:

interstitialAd.setAdListener(new AdListener() {

    public void onAdLoaded() {
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
        public void run() { 

           @Override
           public void onReceiveAd() {
             //Abriu, ta beleza
           }

           @Override
           public void onFailedToReceiveAd(){
            //Dificuldades para abrir
            startActivity();
           }

           @Override
           public void onAdClosed(){
              Log.d("AD Closed", "Ad closed");
              startActivity();
           }

        } 
    }, 5000);
}

});

Will pass 5 seconds of delay, if falling inside onReceiveAd() It is because opened normally, and after being displayed and closed will fall in onAdClosed(), then just send the user to the app home screen.

If you pass the 5 seconds and haven’t loaded the ad yet, the user will fall into onFailedToReceiveAd(), then just send it to the app home screen, the same way as onAdClosed().

Hugs.

  • Hello Leonardo, thank you for the tip, but my problem is that , when the application user is with the weak internet, the ad keeps trying to load infinitely, preventing the application from starting. Do I need to impose a condition of type? If the ad is displayed in 5 seconds (all right), SENAO (cancels ad and opens application)

  • Speak Francis, I changed my answer, try to do it that way now. Hugs.

  • I tried to implement but it didn’t work, I managed to post my code in the question, take a look to see if it gives a light !

0

Try the following:

 private InterstitialAd mInterstitialAd;
    Long exibirAte;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Adicionamos 5 segundos no tempo atual
        Long exibirAte = SystemClock.currentThreadTimeMillis()+5000;
        mInterstitialAd = newInterstitialAd();
        loadInterstitial();

    }


    private InterstitialAd newInterstitialAd() {
        final InterstitialAd interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // se a data atual é maior, então já se passaram 5 segundos...
                if(SystemClock.currentThreadTimeMillis() > exibirAte){
                    showInterstitial();
                }
            }

            // Erro! não conseguiu carregar 
            @Override
            public void onAdFailedToLoad(int errorCode) {}

            @Override
            public void onAdClosed() {
                goToNextLevel();
            }
        });
        return interstitialAd;
    }

    private void showInterstitial() {
        // Mostra o anúncio, se ele está pronto. 
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

    private void loadInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        mInterstitialAd.loadAd(adRequest);
    }

    private void goToNextLevel() {
        mInterstitialAd = newInterstitialAd();
        loadInterstitial();
    }
  • It didn’t work, it’s failing:

  • java.lang.Nullpointerexception: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null Object Reference at com.smapps.testeinterstitialstack.Mainactivity$1.onAdLoaded(Mainactivity.java:35)

  • See if you added this line: Long displayAte = Systemclock.currentThreadTimeMillis()+5000;

  • ok added, but still presenting the error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.