简体   繁体   English

Android应用需要大量时间才能启动

[英]Android app takes lot of time to start

I've an Android app. 我有一个Android应用。 In the onCreate() method of splash screen I've add 在初始屏幕的onCreate()方法中,我添加了

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.i(SplashActivity.APP_NAME, "Executing post-delayed code");
        }
    , 100);

So, what I expect is that this code will get executed after 100ms after onCreate is exited. 因此,我期望的是该代码将在退出onCreate之后100毫秒后执行。

But I can see that my app is taking 3 seconds after onCreate() to execute the post-delayed code (also UI appears after 3seconds): 但是我可以看到我的应用在onCreate()之后需要3秒来执行延迟后的代码(UI也要在3秒后出现):

08-30 19:00:45.614 4559-4559/com.example.my I/My Example: OnCreate is exited
08-30 19:00:45.621 4559-4683/com.example.my D/OpenGLRenderer:   Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-30 19:00:45.654 4559-4683/com.example.my I/Adreno: QUALCOMM build                   : 166ada0, Ifd751822f5
                                                            Build Date                       : 01/26/16
                                                            OpenGL ES Shader Compiler Version: XE031.06.00.05
                                                            Local Branch                     : AU12_SBA
                                                            Remote Branch                    : 
                                                            Remote Branch                    : 
                                                            Reconstruct Branch               : 

**08-30 19:00:45.662** 4559-4683/com.example.my I/OpenGLRenderer: Initialized     EGL, version 1.4

**08-30 19:00:48.646** 4559-4559/com.example.my I/My Example: Executing post-delayed code

Can someone please tell me why an app can take 3 seconds after onCreate() to execute post-delayed code and UI to start appearing? 有人可以告诉我为什么一个应用程序在onCreate()之后可能需要3秒钟来执行延迟后的代码,而UI才开始出现吗?

Please suggest me tips on how I can optimize this time? 请向我建议有关这次如何优化的提示?

One more question , does handler.postDelayed() execute after onStart() / UI is shown? 还有一个问题 ,在显示onStart()/ UI之后是否执行handler.postDelayed()?

Consider to add little larger timeout because even default android studio template set timeout time to 300 ms. 考虑增加一点超时,因为即使默认的android studio模板也将超时时间设置为300毫秒。 You can see the comment in android fullscreen application template 您可以在android全屏应用模板中查看评论

/**
 * Some older devices needs a small delay between UI widget updates
 * and a change of the status and navigation bar.
 */
private static final int UI_ANIMATION_DELAY = 300;

Also much depends on your layout because Android draws it in main thread and if your UI have much nesting levels or hard drawing code it can also reduce performance. 另外,这很大程度上取决于您的布局,因为Android会在主线程中绘制它,并且如果您的UI具有很多嵌套级别或硬绘图代码,它也会降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM