Android get UsageStatsManager in API level 21

情到浓时终转凉″ 提交于 2019-12-06 02:46:10

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;dr
Use Context.getSystemService("usagestats")

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 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!