简体   繁体   English

应用在运行时花费的时间

[英]time the app took while running

I want to calculate the time an app took while running in the foreground activity, 我想计算应用程序在前台活动中运行所花费的时间,

for example : if I opened my fb application on android, can I know the time I opened it and the time I closed the application at it or no? 例如:如果我在android上打开了fb应用程序,是否可以知道我打开它的时间以及关闭它的时间?

I want to know the time of a running app in the front not the background services. 我想知道前端运行应用程序的时间,而不是后台服务的时间。

Thanks. 谢谢。

This is unfortunately a little difficult to do in Android. 不幸的是,这在Android中有点困难。 Generally, this is the idea: 通常,这是一个主意:

  • Every activity's onResume() is called once when that activity appears in front of the user. 当每个活动的onResume()出现在用户面前时,该活动将被调用一次。
  • Every activity's onPause() is called once when that activity disappears. 每个活动的onPause()在该活动消失时都会被调用一次。

What I do in my app is the following: 我在应用程序中执行的操作如下:

  • Every activity in my application becomes the subclass of some GenericActivity 我的应用程序中的每个活动都成为GenericActivity的子类
  • That GenericActivity keeps two arrays, openArray and closeArray 该GenericActivity保留两个数组, openArraycloseArray
  • Every time onResume() is called, getCurrentTimeMillis is pushed onto openArray 每次调用onResume()时, getCurrentTimeMillis都会推送到openArray
  • Every time onPause() is called, getCurrentTimeMillis is pushed onto closeArray 每次调用onPause()时, getCurrentTimeMillis都会推送到closeArray
  • Whenever openArray and closeArray have the same number of elements, that means the app has been closed. 只要openArraycloseArray的元素数相同,就意味着该应用程序已关闭。 Subtract the first element of openArray from the last element of closeArray . 减去的第一个元素openArray从最后一个元素closeArray That should give you the number of milliseconds the application was open in front of the user. 这应该为您提供在用户面前打开应用程序的毫秒数。

Sorry for the longish answer. 对不起,我的回答太长了。 Let me know if this makes any sense. 让我知道这是否有意义。

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

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