UsageStatsService是一个系统服务,其主要通过AMS等,来监测并记录各个应用的使用数据,如上次调用com.android.settings的时间等。
UsageStatsService,a service that collects, aggregates, and persists application usage data. This data can be queried by apps that have been granted permission by AppOps.
代码位置:frameworks/base/services/usage/java/com/android/server/usage/
UsageStatsService创建时,其在onStart()方法中会调用如下方法提供服务,
publishLocalService(UsageStatsManagerInternal.class, new LocalService()); // AMS会调用
publishBinderService(Context.USAGE_STATS_SERVICE, new BinderService()); // 给其他Service和APP调用
其中重点关注LocalService,ActivityManagerService有一个成员变量mUsageStatsService,其会统计4个UsageStatsService自定义的事件(MOVE_TO_FOREGROUND,MOVE_TO_BACKGROUND,CONFIGURATION_CHANGE,SYSTEM_INTERACTION)。mUsageStatsService的赋值在SystemServer#startCoreServices()方法中,如下:
mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class));
二. 事件
数据的事件类型有7种,全部定义在UsageEvents.java中,如下: