Running AD on UI thread

Anything libgdx related goes here!

Running AD on UI thread

Postby temcha » Fri Jan 17, 2020 7:12 pm

Hello,
I've got a game on android and I need to handle next situation:
I've got a game running on the main thread. When player dies, I propose to show ad to continue playing. I need to ran this Ad on UI thread (runOnUiThread(){})
however to receive reward, I want to show popup and it should be done on the main thread
otherwise app can not handle it
so is there a way to use first runOnUiThread, and then call something like getReward() back from the main thread?
I tried to create Runnable on the main thread before calling runOnUiThread, then when the reward is ready I'm starting off this runnable. But it didnt work
any other ideas?
temcha
 
Posts: 1
Joined: Fri Jan 17, 2020 7:08 pm

Re: Running AD on UI thread

Postby shatterblast » Fri Jan 17, 2020 9:05 pm

You need to put your Runnable through a ScheduledThreadPoolExecutor preferably. However, there is a difference between doing it in your Core module and in your AndroidLauncher.

The following code works well if you need to execute something inside the AndroidLauncher itself.
Code: Select all
void androidLauncherMethod() {
   CountDownLatch latch = new CountDownLatch(1);

   class ConnectedFileCreator implements Runnable {
      private final CountDownLatch latch;

      public ConnectedFileCreator(CountDownLatch latch) {
         this.latch = latch;
      }
      @Override
      public void run() {
         System.out.println( "How are things executing with you, my fellow Java methods?" );
         latch.countDown();
      }
   }

   ConnectedFileCreator connectedFileCreator = new ConnectedFileCreator(latch);
   scheduledThreadPoolExecutor.execute(connectedFileCreator);

   try {
      latch.await();
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
}

The above code block helps in the instance where you need to execute network code, like for the Google HTTP Client. In this case, I always create the ScheduledThreadPoolExecutor object in my main Java class that extends ApplicationAdapter. In that way, I can use a getter method in the AndroidLauncher to reference the Executor that is in my main game class.

You always want to keep your ScheduledThreadPoolExecutor in one place and reference it from absolutely everywhere else in your project as necessary. Otherwise, you will find your application crashing. The problem with providing it in the Launcher is that not every platform allows that design. There is no problem using just one Executor in your class that extends ApplicationAdapter within the Core module.

In the next example, I commonly use this form of code block to execute game logic only with the Core module itself.
Code: Select all
void coreGameLogicMethod() {
   class ConnectedFileCreator implements Runnable {
      @Override
      public void run() {
         System.out.println( "How are things executing with you, my fellow Java methods?" );
      }
   }

   ConnectedFileCreator connectedFileCreator = new ConnectedFileCreator();
   scheduledThreadPoolExecutor.execute(connectedFileCreator);
}


The issue with using anything with a Thread, like an Executor, is that your game will likely crash if you switch Viewport and Camera objects somehow. This can happen if you dispose() all of your Stage objects for example and your game has not closed by your player. The following can help in that regard.
Code: Select all
scheduledThreadPoolExecutor.shutdownNow();


However, this will force the need to re-create the ScheduledThreadPoolExecutor object. I believe it absolutely must be done before the creation of any Stage objects in another class file, but I'm actually testing this out for the next few days already.

The schedule() method is also nice for delayed effects, like fading graphics either in or out, but NEVER use it with network code in ANYTHING. At all. Ever. If Elvis Presley comes back from the dead and offers you a new car for the price of using schedule() with network code, tell him to, "Go back where you came from!"

I hope this helps.

1) https://docs.oracle.com/en/java/javase/ ... cutor.html
shatterblast
 
Posts: 653
Joined: Sun Jul 06, 2014 1:14 pm


Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest