应用程序的启动时间

发布于 2024-08-22 07:59:39 字数 57 浏览 10 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尸血腥色 2024-08-29 07:59:39

嗯 - 首先,更准确地说,我应该指出,在 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() and onResume().

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 of onCreate() and end tracing at the end of onResume() with Debug.stopMethodTracing();.

Because onCreate() is only called once per instance, you don't even have to worry about multiple calls to onResume() 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!

折戟 2024-08-29 07:59:39

为了快速完成,我会使用 logcat,例如:

Log.d("tag", "starting");
/* code goes here */
Log.d("tag", "finished");

如果你想做更大的事情,请尝试 Traceview

To do it fast I would use logcat, something like:

Log.d("tag", "starting");
/* code goes here */
Log.d("tag", "finished");

If you want to do something bigger, try Traceview.

装纯掩盖桑 2024-08-29 07:59:39

将会有一个自动日志,例如

system_process I/ActivityManager﹕ Displayed com.android.vending/com.google.android.finsky.activities.MainActivity: +549ms

显示应用程序的启动时间,从用户在应用程序上键入内容到应用程序准备好与用户进一步交互。
这是来自 ActivityManager 的。

另外,使用日志来测量从 onCreate() 到 onResume 的时间应该是另一个好方法。

There will be an automatic log something like

system_process I/ActivityManager﹕ Displayed com.android.vending/com.google.android.finsky.activities.MainActivity: +549ms

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.

執念 2024-08-29 07:59:39

您可以在此处找到答案 -

对于 API 版本 19 或更高版本,此信息默认记录在 Logcat 上。
关键是在正确的位置寻找它 -

如果您从命令行或终端跟踪 logcat 输出,则查找经过的时间非常简单。要在 Android Studio 中查找经过的时间,您必须在 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 -

If you’re tracking logcat output from the command line, or in a terminal, finding the elapsed time is straightforward. To find elapsed time in Android Studio, you must disable filters in your logcat view. Disabling the filters is necessary because the system server, not the app itself, serves this log.

Here is the example from the documentation for completeness.

ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms

The extracts are from the documentation.

回忆追雨的时光 2024-08-29 07:59:39

在上面的答案范围内,我想指出,由于分析时 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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文