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?