How to show interstitial ads after loading the home screen?

Asked

Viewed 302 times

0

I need help, I would like to display an interstitial ad after the home screen loads, but my code is giving the following failure:

java.lang.Illegalstateexception: isLoaded must be called on the main UI thread.

Here is my code:

public class SplashScreenActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        // FIREBASE INTERSTICIAL
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-2565065222479596/3931476543");

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });

        requestNewInterstitial();

        Toast.makeText(this,"* Necessário Acesso a Internet *",Toast.LENGTH_LONG).show();

        Thread timerThread = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {

                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    else
                    {
                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                    }
                }

            }
        };
        timerThread.start();

    }

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

        mInterstitialAd.loadAd(adRequest);
    }

}

DEBUG:

    01-22 16:27:03.048 13840-13970/? E/AndroidRuntime: FATAL EXCEPTION: Thread-6
    Process: idea.tisco.pepavideos, PID: 13840
    java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
    at oc.b(:com.google.android.gms.DynamiteModulesA@11951448:20)
    at com.google.android.gms.ads.internal.a.d(:com.google.android.gms.DynamiteModulesA@11951448:98)
    at com.google.android.gms.ads.internal.client.ak.onTransact(:com.google.android.gms.DynamiteModulesA@11951448:14)
    at android.os.Binder.transact(Binder.java:499)
    at com.google.android.gms.internal.zzep$zza$zza.isReady(Unknown Source)
    at com.google.android.gms.internal.zzfa.isLoaded(Unknown Source)
    at com.google.android.gms.ads.InterstitialAd.isLoaded(Unknown Source)
    at company.ts.SplashScreenActivity$2.run(SplashScreenActivity.java:50)

Even after debugging, I couldn’t understand the reason for the error.

I would just like to display an interstitial right after the home screen or when I open the main activity.

Thank you very much!

  • It’s worth remembering that starting an interstitial right after opening the home screen hurts Google’s rules. You may have your account suspended because of this. https://support.google.com/admob/answer/6201362?hl=pt-BR&ref_topic=2745287

1 answer

1


When you create a thread on Android, you nay can modify the thread user interface directly. It is necessary to use the method runOnUiThread.

Thus:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
});

And to carry the InterstitialAd, just add the code below in your method onCreate

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-2565065222479596/3931476543");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
  • Thanks Valdeir. It worked, no more error appeared. But I do not know why the interticial did not appear, it always falls in Else.

  • runOnUiThread(new Runnable() { @Override public void run() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); Intent Intent = new Intent(Splashscreenactivity.this, Mainactivity.class); startActivity(Intent); } Else {}

  • As the banner has not yet been loaded the condition if (mInterstitialAd.isLoaded()) { will always be false. I edited my answer.

  • Thank you very much, Valdeir. Perfect!!! Thank you for contributing your knowledge, helped me so much. Hug!

Browser other questions tagged

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