如何获得“当日"的使用情况统计信息?在android(kotlin)中使用usageStatsManager [英] How to get usage stats for "current day" using usageStatsManager in android (kotlin)
问题描述
目标:
需要获取今天的使用情况统计信息(今天使用该设备的总时间),即.当前时间是凌晨12点.
问题:
1.我得到今天的时间+其他无法解释的时间
2.不可解释的时间戳(通过getTimestamp方法检索的使用状态的开始和结束)
时间段无关紧要.我将开始时间定为上午12.00,将结束时间定为当前时间,但是对于使用情况统计信息,我得到的是完全不相关的".firstTimeStamp"和".lastTimeStamp"(它们应该返回使用情况统计数据的开始和结束). /p>
*已经完成了权限授予部分,这是我用来获取总时间(以分钟为单位)的函数.
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日星期二GMT +05:30到
2018年12月11日星期二19:38:45 GMT +05:30
如何在短短40分钟内使用手机90分钟?
那些显然无关紧要的时间戳意味着什么?
为了实现我的目标,我做错了什么吗?
我实际上遇到了类似的问题:
据我对文档firstTimeStamp
和lastTimeStamp
的理解,应给出该UsageStats表示的时间范围的开始(结束)".
但是,它们与我在queryAndAggregateUsageStats
中作为参数beginTime
和endTime
给出的参数不同.
totalTimeInForeground
的结果似乎比由firstTimeStamp
/lastTimeStamp
给出的时间间隔返回结果,而不是返回所请求的结果.
我为此给Google填充了一个错误,请查看 https://issuetracker.google .com/issues/118564471 .
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.
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)}")
}
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
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:
According to my understanding of the documentation firstTimeStamp
and lastTimeStamp
should give the "beginning (end) of the time range this UsageStats represents".
They differ however from what I give as an argument in queryAndAggregateUsageStats
as beginTime
and endTime
.
Also the result for the totalTimeInForeground
seems rather give back a result for the timespan given by firstTimeStamp
/ lastTimeStamp
than for the requested one.
I filled a bug with google for this, please have a look at https://issuetracker.google.com/issues/118564471.
这篇关于如何获得“当日"的使用情况统计信息?在android(kotlin)中使用usageStatsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!