Handler.sendMessageDelayed(味精,延迟)无法正常工作味精、无法正常、工作、Handler

2023-09-07 22:09:48 作者:♂命里缺個Ní♀

我已经定义加载过程中要显示一个启动画面。但根据Internet连接上它可以只需要600毫秒加载或有时5000毫秒。所以我定义的启动画面被至少显示3000ms从而用户不是通过flackering屏幕激怒。

I have defined a splashscreen to be shown during loading. But depending on the internet connection it can takes only 600ms to load or sometimes 5000ms. So i defined that the splashscreen is at least shown 3000ms so that the user is not irritated by flackering screen.

我定义启动画面的启动方式如下:

I define the start of the splashscreen the following way:

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

在LoadingThread我从网上查看网络和​​负载数据:

In the LoadingThread I check the network and load data from the Internet:

private class LoadingThread extends Thread {

        @Override
        public void run() {
            checkNetwork();
        }

    }

一旦加载完成后,我发信息给在MainActivity定义我的处理程序:

As soon as loading is done, i send a message to my handler defined in the 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);
        }
    }

在LoadingThreads一些code线由runOnUIThread()调用。不幸的是,如果时间&LT; SPLASH_MIN_TIME消息不推迟,但即时发送。我觉得跟sendMessageDelayed()这不应该是这样的。任何人都知道这是为什么?该系统输出表示延迟时间被正确地计算。谢谢!

Some code lines on the LoadingThreads are called by runOnUIThread(). Unfortunately, if time < SPLASH_MIN_TIME the message isn't delayed but send instantly. I think with sendMessageDelayed() this shouldn't be the case. Anybody knows why? The sysout shows that the delay time is calculated correctly. Thanks!

推荐答案

也许是错误不是在拖延。 (不知道没有更多的code)但可能的原因是:你的下载线程您的下载后显示另一个布局和启动画面的顶层下不可见。延迟后启动画面收到你的信息,所以你没有看到它后面。

maybe the error not in the delay. (don't know without more code) but the possible reason is: your download thread displaying another layout after your download, and splashscreen become invisible under your top layer. Splashscreen receive your message after delay, therefore you dont see it later.