我有一个Android应用程序。在启动屏幕的oncreate()
方法中,我添加了
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.i(SplashActivity.APP_NAME, "Executing post-delayed code");
}
, 100);
因此,我希望在onCreate退出后100ms后执行该代码。
但我可以看到,我的应用程序在onCreate()之后花了3秒时间来执行延迟后的代码(在3秒之后还会出现UI):
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
有人能告诉我为什么一个应用程序可以在onCreate()之后花3秒来执行延迟后的代码和UI开始出现?
请建议我如何优化这一次的技巧?
还有一个问题,Handler.PostDelayed()在显示onStart()/UI之后执行吗?
考虑增加一点较大的超时,因为即使是默认的android studio模板也将超时时间设置为300毫秒。您可以在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;
另外,很大程度上取决于您的布局,因为Android是在主线程中绘制的,如果您的UI有很多嵌套级别或硬绘制代码,也会降低性能。