问题
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?
回答1:
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.
回答2:
I noticed several problems with your approach.
- You are missing
time.set(Calendar.SECOND,0)
andtime.set(Calendar.MILLISECOND,0)
- Precision is lost in the division
ft=value.totalTimeInForeground/60000
I would recommend Java Time (ThreeTenBP) to handle DateTime and Duration more accurately. I create a new function to compare and indeed the results are different.
fun showtime2(){
val start = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
val end = ZonedDateTime.now().toInstant().toEpochMilli()
val usageStatsManager = getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val stats = usageStatsManager.queryAndAggregateUsageStats(start, end)
val total = Duration.ofMillis(stats.values.map { it.totalTimeInForeground }.sum())
println("YOU SPENT ${total.toMinutes()} mins.")
}
Your output
YOU SPENT 577 mins.
My output
YOU SPENT 582 mins.
来源:https://stackoverflow.com/questions/53730599/how-to-get-usage-stats-for-current-day-using-usagestatsmanager-in-android-kot