Get Android App Start Time in a non-activity class

江枫思渺然 提交于 2019-12-23 02:38:11

问题


I want to get the start datetime of the android application. 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. 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.

Considering this scenario, is there any way to do this?


回答1:


getElapsedCpuTime() in android.os.Process should give you what you want:

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.




回答2:


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.

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.



来源:https://stackoverflow.com/questions/30316752/get-android-app-start-time-in-a-non-activity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!