tl;dr:我想准时获取今天、昨天或过去任何日期的屏幕。
我似乎无法找到如何在 Android 上的应用程序中获取与设备屏幕时间相关的统计数据。我遇到过 UsageStats API,但它返回不同应用程序的使用统计信息。
我知道它可以在任何 Android 设备上完成(也许来自特定版本的 Android),因为有一个名为 ActionDash 的应用程序具有此功能。有人可以告诉我怎么做吗?
您可以使用 UsageStatsManager 通过添加每个应用程序的所有使用统计信息来计算屏幕时间,这将是总屏幕时间。
检查下一个示例:
class UsageApi(
private val applicationContext: Context
) {
val usageStatsManager: UsageStatsManager?
get() = if (checkForPermission()) {
applicationContext.getSystemService(Activity.USAGE_STATS_SERVICE) as? UsageStatsManager
} else null
fun checkForPermission(): Boolean {
val appOps = applicationContext.getSystemService(Activity.APP_OPS_SERVICE) as AppOpsManager
val mode =
appOps.checkOpNoThrow(
AppOpsManager.OPSTR_GET_USAGE_STATS,
Process.myUid(),
applicationContext.packageName
)
return mode == AppOpsManager.MODE_ALLOWED
}
fun getUsageStats(): List<UsageStats> {
val startOfWeekInMillis = TimeUtils.getStartOfWeekInMillis()
val currentTimeInMillis = System.currentTimeMillis()
return usageStatsManager
?.queryUsageStats(
UsageStatsManager.INTERVAL_WEEKLY,
startOfWeekInMillis,
currentTimeInMillis
)
?.filter { it.totalTimeInForeground > 0 }
?.filter { it.packageName != applicationContext.packageName }
?.sortedByDescending { it.totalTimeInForeground }
?: emptyList()
}
}
我定义了一个UsageState类来帮助我存储usageStatsList:
data class UsageState(
val usageStatsList: List<UsageStats> = emptyList(),
val permissionGranted: Boolean = false,
) {
val formattedDuration: String =
TimeUtils.formatDuration(usageStatsList.sumOf { it.totalTimeInForeground })
}
并定义了一个TimeUtils来格式化时间:
object TimeUtils {
fun formatDuration(durationInMilliseconds: Long): String {
var remainingSeconds = durationInMilliseconds / 1000
val days = remainingSeconds / 86_400
remainingSeconds -= days * 86_400
val hours = remainingSeconds / 3_600
remainingSeconds -= hours * 3_600
val minutes = remainingSeconds / 60
remainingSeconds -= minutes * 60
val seconds = remainingSeconds
return (if (days > 0) "$days día${if (days > 1) "s" else ""} "
else "") + (if (hours > 0) "$hours hora${if (hours > 1) "s" else ""} "
else "") + (if (minutes > 0) "$minutes minuto${if (minutes > 1) "s" else ""} "
else "") + (if (seconds > 0) "$seconds segundo${if (seconds > 1) "s" else ""} "
else "")
}
fun getStartOfWeekInMillis(): Long {
val calendar: Calendar = Calendar.getInstance()
calendar.set(Calendar.DAY_OF_WEEK, calendar.firstDayOfWeek)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MILLISECOND, 0)
return calendar.timeInMillis
}
fun getDayOfTheWeek(): Int {
val calendar = Calendar.getInstance()
return calendar[Calendar.DAY_OF_WEEK]
}
fun getDayOfTheWeekAsString(): String {
val daysOfWeek = listOf("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado")
return daysOfWeek[getDayOfTheWeek() - 1]
}
fun getAverageDailyUsage(usageStatsList: List<UsageStats>): Long {
if (usageStatsList.isEmpty()) return 0L
val today = getDayOfTheWeek()
val totalUsage = usageStatsList.sumOf { it.totalTimeInForeground }
return totalUsage / today
}
}
请告诉我这是否适合您,并随时分享任何反馈。