Handler.sendMessageDelayed (msg, delay) is not working correctly

I defined a pop-up screen that will be displayed at boot time. But depending on the Internet connection, it may take only 600 ms to download, and sometimes 5000 ms. Thus, I determined that the splash screen was at least shown at 3000 ms so that the user was not annoyed by the flex screen.

I define the startup of the screen saver as follows:

private void splashScreen() { setContentView(R.layout.splashscreen); splash = (ImageView) findViewById(R.id.splashscreenLayer); startSplashTime = new Date(); new LoadingThread().start(); } 

In LoadThread, I check the network and download data from the Internet:

 private class LoadingThread extends Thread { @Override public void run() { checkNetwork(); } } 

Once the download is complete, I send a message to my handler, defined in MainActivity:

 public void stopSplash() { Message msg = new Message(); msg.what = STOPSPLASH; Date endSplashTime = new Date(); long time = endSplashTime.getTime() - startSplashTime.getTime(); System.out.println("Time Splashscreen was displayed: " + time); if (time < SPLASH_MIN_TIME) { long delay = SPLASH_MIN_TIME - time; System.out.println("Delay Splashscreen for: " + delay); splashHandler.sendMessageDelayed(msg, delay); } else { System.out.print("Show Splashscreen now"); splashHandler.sendMessage(msg); } } 

Some lines of code on LoadThreads are called by runOnUIThread (). Unfortunately, if the time <SPLASH_MIN_TIME, the message is not delayed, but is sent instantly. I think with sendMessageDelayed () this should not be so. Does anyone know why? Sysout indicates that the delay time is calculated correctly. Thanks!

+6
android multithreading handler android-handler
source share
1 answer

perhaps the error is not a delay. (I don’t know without code), but the possible reason is as follows: your download stream, displaying a different layout after loading, and the pop-up window becomes invisible under your top layer. Splashscreen receives your message after a delay, so you do not see it later.

0
source share

All Articles