droid在API级别21中获得UsageStatsManage

droid在API级别21中获得UsageStatsManage

本文介绍了Android在API级别21中获得UsageStatsManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取有关最近使用的应用程序的信息.

I need to get info about recent apps usage.

在使用API​​ 21之前,已使用 getRunningTasks getRecentTasks 即可获取此信息.根据文档,API 21中已弃用这些方法.

Before API 21 it was used getRunningTasks or getRecentTasks to get this info. These methods were deprecated in API 21 according docs.

现在有 UsageStatsManager 类,已在API级别引入21.
但是! Context.getSystemService(UsageStatsService) 仅在API级别22中添加!

There is UsageStatsManager class now, introduced in API level 21.
But! Context.getSystemService(UsageStatsService) was added only in API level 22!

问题是-如何在API 21中获取所需数据 ??根据仪表板,在此API级别上有13%的用户,我不想失去他们.

The question is - how to get needed data in API 21?? According dashboards, there is 13% of users at this API level, I don't wanna lose them.

推荐答案

在API级别1中引入了Context#getSystemService(...),但在API级别22中引入了Contexts的USAGE_STATS_SERVICE字符串.因此, public静态最终字符串字段Context.USAGE_STATS_SERVICE只能在API级别为22以上的设备上访问.为了在API级别21中获得所需的数据,您只需要将解析后的字符串"usagestats"传递给此方法.

Well Context#getSystemService(...) was introduced in API level 1 but Contexts' USAGE_STATS_SERVICE String was introduced in API level 22. So the public static final String field Context.USAGE_STATS_SERVICE can only be accessed on devices with API level 22 and up. In order to get the needed data in API level 21 you simply need to pass to this method the resolved String "usagestats".

tl;博士
使用Context.getSystemService("usagestats")

更多说明:
#1 USAGE_STATS_SERVICE字段已经在API级别21中,但已标记为@hide属性,该属性使该属性不可访问.在API级别22中已将其删除.

Further notes:
#1 The USAGE_STATS_SERVICE field was already in API level 21 but was tagged with the @hide property which made in inaccessible. In API level 22 this was removed.

#2您可能想通过@SuppressWarnings("WrongConstant")转到给定方法的属性检查,否则Android Studio会抱怨它不是有效的常量

#2 You may want to turn of the attribution inspection for the given method via @SuppressWarnings("WrongConstant") otherwise Android Studio will complaint that it isn't a valid constant

这篇关于Android在API级别21中获得UsageStatsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:04