如何获取“当天"的使用统计信息在 android (kotlin) 中使用 usageStatsManager [英] How to get usage stats for "current day" using usageStatsManager in android (kotlin)
问题描述
目标:
需要获取今天的使用统计信息(今天使用设备的总时间),即.上午 12 点到当前时间.
Objective:
Need to get the usage stats for today (total time for which the device was used today) ie. 12.00 am to current time.
问题:
1.我得到今天的时间+其他一些不可解释的时间
2.不可解释的时间戳(getTimestamp 方法检索到的使用统计的开始和结束)
时间段不相关.我将开始时间指定为上午 12 点,将结束时间指定为当前时间,但是对于使用情况统计数据,我得到了完全不相关的.firstTimeStamp"和.lastTimeStamp"(它们应该返回使用情况统计数据的开始和结束时间).
Problem:
1.I get today's time + some other non explainable time
2.Non explainable time stamps(start and end of the usage stats as retrieved by the getTimestamp methods)
The time bucket is not relevant. I give the start time as 12.00 am and the end time as current time, but I get completely irrelevant ".firstTimeStamp" and ".lastTimeStamp" (which supposedly return the beginning and end of the usage stats data) for the usage statistics.
*已经完成了权限授予部分,这是我用来以分钟为单位获取总时间的函数.
*already done the permission granting part, here is the function I'm using to get total time in minutes.
fun showtime(){
val time=Calendar.getInstance()
time.set(Calendar.HOUR_OF_DAY,0)
time.set(Calendar.MINUTE,0)
val start=time.timeInMillis
val end= System.currentTimeMillis()
val usageStatsManager = getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
var stats = usageStatsManager.queryAndAggregateUsageStats(start,end)
var x:Long=0
var ft:Long=0
var v:Long=0
var l:Long=0
for ((key,value) in stats) {
ft=value.totalTimeInForeground/60000
textField1.append("$key = $ft mins")
textField1.append("\n")
x=x+ft
v=value.firstTimeStamp
l=value.lastTimeStamp
}
textView.setText("YOU SPENT $x mins.")
textView2.setText("${Date(v)} to \n${Date(l)}")
}
举个例子,当上面的代码在 12 月 12 日星期三 12:40 am 运行时,结果是:
(在 textView 中):
你花了 90 分钟
(在 textView2 中):
2018 年 12 月 11 日星期二 16:23:19 GMT +05:30 到
2018 年 12 月 11 日星期二 19:38:45 GMT +05:30
As an example, when the above code runs at Wed 12 Dec 12.40 am, the result is:
(in textView):
YOU SPENT 90 mins
(in textView2):
Tue 11 Dec 16:23:19 GMT +05:30 2018 to
Tue 11 Dec 19:38:45 GMT +05:30 2018
如何在 40 分钟内使用手机 90 分钟?
那些明显不相关的时间戳是什么意思?
我在实现目标方面做错了什么吗?
How can I use my phone for 90 mins in just 40 mins?
And what does those apparently irrelevant timestamps mean?
Am I doing something wrong to achieve my objective?
推荐答案
其实我也遇到过类似的问题:
I actually experience a similar problem:
根据我对文档的理解 firstTimeStamp
和 lastTimeStamp
应该给出这个 UsageStats 代表的时间范围的开始(结束)".
According to my understanding of the documentation firstTimeStamp
and lastTimeStamp
should give the "beginning (end) of the time range this UsageStats represents".
然而,它们与我在 queryAndAggregateUsageStats
中作为参数给出的不同,如 beginTime
和 endTime
.
They differ however from what I give as an argument in queryAndAggregateUsageStats
as beginTime
and endTime
.
此外,totalTimeInForeground
的结果似乎不如返回由 firstTimeStamp
/lastTimeStamp
给出的时间跨度的结果,而不是请求的结果.
Also the result for the totalTimeInForeground
seems rather give back a result for the timespan given by firstTimeStamp
/ lastTimeStamp
than for the requested one.
我为此用谷歌填充了一个错误,请查看 https://issuetracker.google.com/issues/118564471.
I filled a bug with google for this, please have a look at https://issuetracker.google.com/issues/118564471.
这篇关于如何获取“当天"的使用统计信息在 android (kotlin) 中使用 usageStatsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!