简体   繁体   English

在非活动类中获取Android应用启动时间

[英]Get Android App Start Time in a non-activity class

I want to get the start datetime of the android application. 我想获取android应用程序的开始日期时间。 The simplest way to do is to note the current time in the onCreate of activity class, but my issue is that I am creating an android library with some utility functions. 最简单的方法是在活动类的onCreate上记录当前时间,但是我的问题是我正在创建带有一些实用程序功能的android库。 I want to find out the start datetime of end user application (that is using the library) within the library itself. 我想找出库本身内的最终用户应用程序(即使用库)的开始日期时间。 I don't want to bound the app developer to note down the start time in the onCreate method and pass to the library method. 我不想让应用程序开发人员在onCreate方法中记下开始时间并传递给库方法。

Considering this scenario, is there any way to do this? 考虑到这种情况,有没有办法做到这一点?

getElapsedCpuTime() in android.os.Process should give you what you want: android.os.Process中的getElapsedCpuTime()应该给您您想要的东西:

http://developer.android.com/reference/android/os/Process.html#getElapsedCpuTime() http://developer.android.com/reference/android/os/Process.html#getElapsedCpuTime()

It's a static method that should by default return the elapsed cpu time for the calling process. 这是一个静态方法,默认情况下应返回调用过程所用的cpu时间。

How about writing a public method( like getStartDateTime() ) in your library to determine the launchtime in your library, and invoking it from the application class of your main application. 如何在您的库中编写一个公共方法(如getStartDateTime())来确定库中的启动时间,然后从主应用程序的应用程序类中调用它。

By specifying application class name in your AndroidManifest.xml's tag, the application class will be instantiated for you when the process for your application/package is created, which will in turn invoke the getStartDateTime() in your library. 通过在AndroidManifest.xml的标签中指定应用程序类名称,将在创建应用程序/包的过程时为您实例化该应用程序类,该过程随后将在您的库中调用getStartDateTime()。

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

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