Android - queryAndAggregateUsageStats returns empty map

189 views Asked by At

I am working on a requirement where I need to get the app usage every 15 mins. I am getting empty map. Please see below code. I guess INTERVAL_BEST interval type is not working for given timeframe. Any help will be appreciated. Thanks.

 fun getAppsUsageMap() {
    val manager: UsageStatsManager? = null
    val calendar = Calendar.getInstance()
    val startTime = calendar.timeInMillis;
    calendar.add(Calendar.MINUTE, -15)
    val endTime = calendar.timeInMillis;
    System.out.println("Current Date in milSec = " + startTime)
    System.out.println("Updated Date in milSec = " + endTime)
    try {
        val usageStatsMap = manager?.queryAndAggregateUsageStats(startTime, endTime)

        if (usageStatsMap != null) {
            System.out.println("map size   " + usageStatsMap.size)
            usageStatsMap.forEach { (key, value) -> System.out.println("$key = $value") }
        }
    } catch (e: Exception) {
        Log.d("TAG", "Getting resulted in an exception")
    }
}
1

There are 1 answers

0
Yuna On

endTime should be bigger than startTime. Based on your code, your endTime is actually 15 minutes smaller than startTime.