应用程序的启动时间
在android中启动应用程序并计算其启动时间的最佳方法是什么(如果可以用一些代码完成,那就更好了)
What is the best way to launch an app and calculate its launch time in android(if it can be done with some code,then its better)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯 - 首先,更准确地说,我应该指出,在 Android 中,您启动的是 Activity,而不是应用程序!
因此,由于应用程序的入口点是处理 LAUNCH 意图的 Activity,因此人们可以将您的问题解释为“如何测量 Activity 启动时间”。
为此,我建议在这里查看活动生命周期: https:// /developer.android.com/reference/android/app/Activity.html#ActivityLifecycle。
查看那里的漂亮图表,您会发现启动时间本质上是在
onCreate()
、onStart()
和onResume()
中花费的时间代码>.为了衡量这一点,我建议使用 traceview 因为这会真正告诉你详细了解您在哪里度过的时间!在
onCreate()
开头使用Debug.startMethodTracing("startUp");
开始跟踪,并在onResume()
末尾结束跟踪使用Debug.stopMethodTracing();
。由于每个实例仅调用
onCreate()
一次,因此您甚至不必担心多次调用onResume()
以防此 Activity 被置于后台因为您将调用 stop 方法两次,这没有什么坏处!玩得开心 - 我非常喜欢 Traceview 的可能性!
Hmm - first, to be more precise, I should point out that in Android you start activities, rather than applications!
So, as your entry point to your application is the Activity which handles the LAUNCH intent, one could interpret your question as "how to measure activity start up time".
For this, I suggest to look at an Activities lifecycle here: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle.
Looking at the nice graph there, you see that startup time is essentially the time that is spent in
onCreate()
,onStart()
andonResume()
.To measure that, I would suggest to use traceview as this will really show you in all its detail where you spent your time! Start tracing using
Debug.startMethodTracing("startUp");
in the beginning ofonCreate()
and end tracing at the end ofonResume()
withDebug.stopMethodTracing();
.Because
onCreate()
is only called once per instance, you don't even have to worry about multiple calls toonResume()
in case this activity will be put to the background as you will call the stop method twice, which is no harm!Have fun - I like the possibilities of traceview very much!
为了快速完成,我会使用 logcat,例如:
如果你想做更大的事情,请尝试 Traceview。
To do it fast I would use logcat, something like:
If you want to do something bigger, try Traceview.
将会有一个自动日志,例如
显示应用程序的启动时间,从用户在应用程序上键入内容到应用程序准备好与用户进一步交互。
这是来自 ActivityManager 的。
另外,使用日志来测量从 onCreate() 到 onResume 的时间应该是另一个好方法。
There will be an automatic log something like
to show the launch time of an app from the user typing on it to the app be ready to interact with user further more.
That is from ActivityManager.
Also, using log to measure the time from onCreate() to onResume should be another good way.
您可以在此处找到答案 -
对于 API 版本 19 或更高版本,此信息默认记录在 Logcat 上。
关键是在正确的位置寻找它 -
为了完整性,以下是文档中的示例。
ActivityManager:显示 com.android.myexample/.StartupTiming:+3s534ms
摘录自 文档。
You could find the answer here -
This information gets logged on Logcat by default for API version 19 or higher.
The key is looking for it in the right place -
Here is the example from the documentation for completeness.
ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms
The extracts are from the documentation.
在上面的答案范围内,我想指出,由于分析时 JIT 被关闭,Traceview 无法提供实时信息。 Traceview 是监控源代码执行时间的无用工具。 (Traceview 是一个仅检查堆栈跟踪的好工具,如上所述,通过使用带有 startMethodTracing() 和 stopMethodTracing() 的 Debug 类)。
我建议使用 systrace(例如通过 Eclipse 插件),这是计算实时执行时间(以及许多其他功能)的最佳工具。
欲了解更多信息,请查看:
http://developer.android.com/tools/debugging/systrace.html
另外,我想指出,有时 systrace 不起作用(取决于 FS 映射)。
您需要检查'system\core\rootdir\init.rc'是否正确安装了'debugfs'。应为:'mount debugfs /sys/kernel/debug /sys/kernel/debug'
In scope of answers above I would like to note, what the Traceview unable to provide the real time, due to JIT is turned off while profiling. The Traceview is the useless tool to monitor the time of executing of source's code. (Traceview is a good tools to check stacktrace only, as mentioned above by using Debug class with startMethodTracing() and stopMethodTracing()).
I suggest to use systrace (via Eclipse plugin, for an example) this is best tool to calculate the real time of executing (and many other features).
For more information take a look at:
http://developer.android.com/tools/debugging/systrace.html
Also, I want to note that sometimes systrace will not work (depends from FS mapping).
You need to check 'system\core\rootdir\init.rc' with correct of 'debugfs' mounted. Should be as: 'mount debugfs /sys/kernel/debug /sys/kernel/debug'