问题
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