Added in API level 21

UsageStatsManager


class UsageStatsManager
kotlin.Any
   ↳ android.app.usage.UsageStatsManager

Provides access to device usage history and statistics. Usage data is aggregated into time intervals: days, weeks, months, and years.

When requesting usage data since a particular time, the request might look something like this:
PAST                   REQUEST_TIME                    TODAY                   FUTURE
  ————————————————————————————||———————————————————————————¦-----------------------|
                         YEAR ||                           ¦                       |
  ————————————————————————————||———————————————————————————¦-----------------------|
   MONTH            |         ||                MONTH      ¦                       |
  ——————————————————|—————————||———————————————————————————¦-----------------------|
    |      WEEK     |     WEEK||    |     WEEK     |     WE¦EK     |      WEEK     |
  ————————————————————————————||———————————————————|———————¦-----------------------|
                              ||           |DAY|DAY|DAY|DAY¦DAY|DAY|DAY|DAY|DAY|DAY|
  ————————————————————————————||———————————————————————————¦-----------------------|
  
A request for data in the middle of a time interval will include that interval.

NOTE: Most methods on this API require the permission android.permission.PACKAGE_USAGE_STATS. However, declaring the permission implies intention to use the API and the user of the device still needs to grant permission through the Settings application. See android.provider.Settings#ACTION_USAGE_ACCESS_SETTINGS. Methods which only return the information for the calling package do not require this permission. E.g. getAppStandbyBucket() and queryEventsForSelf(long,long).

Summary

Constants
static String

A String extra, when used with UsageEvents.Event.getExtras, that indicates the action of the user interaction associated with the event.

static String

A String extra, when used with UsageEvents.Event.getExtras, that indicates the category of the user interaction associated with the event.

static Int

An interval type that will use the best fit interval for the given time range.

static Int

An interval type that spans a day.

static Int

An interval type that spans a month.

static Int

An interval type that spans a week.

static Int

An interval type that spans a year.

static Int

The app was used very recently, currently in use or likely to be used very soon.

static Int

The app was used in the last few days and/or likely to be used in the next few days.

static Int

The app has not be used for several days and/or is unlikely to be used for several days.

static Int

The app has not be used for several days, is unlikely to be used for several days, and has been misbehaving in some manner.

static Int

The app was used recently and/or likely to be used in the next few hours.

Public methods
Int

Returns the current standby bucket of the calling app.

Boolean
isAppInactive(packageName: String!)

Returns whether the specified app is currently considered inactive.

MutableMap<String!, UsageStats!>!
queryAndAggregateUsageStats(beginTime: Long, endTime: Long)

A convenience method that queries for all stats in the given range (using the best interval for that range), merges the resulting data, and keys it by package name.

MutableList<ConfigurationStats!>!
queryConfigurations(intervalType: Int, beginTime: Long, endTime: Long)

Gets the hardware configurations the device was in for the given time range, aggregated by the specified interval.

MutableList<EventStats!>!
queryEventStats(intervalType: Int, beginTime: Long, endTime: Long)

Gets aggregated event stats for the given time range, aggregated by the specified interval.

UsageEvents?

Query for events with specific UsageEventsQuery object.

UsageEvents!
queryEvents(beginTime: Long, endTime: Long)

Query for events in the given time range.

UsageEvents!
queryEventsForSelf(beginTime: Long, endTime: Long)

Like queryEvents(long,long), but only returns events for the calling package.

MutableList<UsageStats!>!
queryUsageStats(intervalType: Int, beginTime: Long, endTime: Long)

Gets application usage stats for the given time range, aggregated by the specified interval.

Constants

EXTRA_EVENT_ACTION

Added in API level 35
static val EXTRA_EVENT_ACTION: String

A String extra, when used with UsageEvents.Event.getExtras, that indicates the action of the user interaction associated with the event. The action cannot be more than 127 characters, longer value will be truncated to 127 characters.

Value: "android.app.usage.extra.EVENT_ACTION"

EXTRA_EVENT_CATEGORY

Added in API level 35
static val EXTRA_EVENT_CATEGORY: String

A String extra, when used with UsageEvents.Event.getExtras, that indicates the category of the user interaction associated with the event. The category cannot be more than 127 characters, longer value will be truncated to 127 characters.

Value: "android.app.usage.extra.EVENT_CATEGORY"

INTERVAL_BEST

Added in API level 21
static val INTERVAL_BEST: Int

An interval type that will use the best fit interval for the given time range. See queryUsageStats(int,long,long).

Value: 4

INTERVAL_DAILY

Added in API level 21
static val INTERVAL_DAILY: Int

An interval type that spans a day. See queryUsageStats(int,long,long).

Value: 0

INTERVAL_MONTHLY

Added in API level 21
static val INTERVAL_MONTHLY: Int

An interval type that spans a month. See queryUsageStats(int,long,long).

Value: 2

INTERVAL_WEEKLY

Added in API level 21
static val INTERVAL_WEEKLY: Int

An interval type that spans a week. See queryUsageStats(int,long,long).

Value: 1

INTERVAL_YEARLY

Added in API level 21
static val INTERVAL_YEARLY: Int

An interval type that spans a year. See queryUsageStats(int,long,long).

Value: 3

STANDBY_BUCKET_ACTIVE

Added in API level 28
static val STANDBY_BUCKET_ACTIVE: Int

The app was used very recently, currently in use or likely to be used very soon. Standby bucket values that are ≤ STANDBY_BUCKET_ACTIVE will not be throttled by the system while they are in this bucket. Buckets > STANDBY_BUCKET_ACTIVE will most likely be restricted in some way. For instance, jobs and alarms may be deferred.

Value: 10

STANDBY_BUCKET_FREQUENT

Added in API level 28
static val STANDBY_BUCKET_FREQUENT: Int

The app was used in the last few days and/or likely to be used in the next few days. Restrictions will apply to these apps, such as deferral of jobs and alarms. The delays may be greater than for apps in higher buckets (lower bucket value). Bucket values > STANDBY_BUCKET_FREQUENT may additionally have network access limited.

Value: 30

STANDBY_BUCKET_RARE

Added in API level 28
static val STANDBY_BUCKET_RARE: Int

The app has not be used for several days and/or is unlikely to be used for several days. Apps in this bucket will have more restrictions, including network restrictions, except during certain short periods (at a minimum, once a day) when they are allowed to execute jobs, access the network, etc.

Value: 40

STANDBY_BUCKET_RESTRICTED

Added in API level 30
static val STANDBY_BUCKET_RESTRICTED: Int

The app has not be used for several days, is unlikely to be used for several days, and has been misbehaving in some manner. Apps in this bucket will have the most restrictions, including network restrictions and additional restrictions on jobs.

Note: this bucket is not enabled in Build.VERSION_CODES.R.

Value: 45

STANDBY_BUCKET_WORKING_SET

Added in API level 28
static val STANDBY_BUCKET_WORKING_SET: Int

The app was used recently and/or likely to be used in the next few hours. Restrictions will apply to these apps, such as deferral of jobs and alarms.

Value: 20

Public methods

getAppStandbyBucket

Added in API level 28
fun getAppStandbyBucket(): Int

Returns the current standby bucket of the calling app. The system determines the standby state of the app based on app usage patterns. Standby buckets determine how much an app will be restricted from running background tasks such as jobs and alarms.

Restrictions increase progressively from STANDBY_BUCKET_ACTIVE to STANDBY_BUCKET_RESTRICTED, with STANDBY_BUCKET_ACTIVE being the least restrictive. The battery level of the device might also affect the restrictions.

Apps in buckets ≤ STANDBY_BUCKET_ACTIVE have no standby restrictions imposed. Apps in buckets > STANDBY_BUCKET_FREQUENT may have network access restricted when running in the background.

The standby state of an app can change at any time either due to a user interaction or a system interaction or some algorithm determining that the app can be restricted for a period of time before the user has a need for it.

You can also query the recent history of standby bucket changes by calling queryEventsForSelf(long,long) and searching for UsageEvents.Event.STANDBY_BUCKET_CHANGED.

Return
Int the current standby bucket of the calling app. One of STANDBY_BUCKET_* constants. Value is android.app.usage.UsageStatsManager.STANDBY_BUCKET_EXEMPTED, android.app.usage.UsageStatsManager#STANDBY_BUCKET_ACTIVE, android.app.usage.UsageStatsManager#STANDBY_BUCKET_WORKING_SET, android.app.usage.UsageStatsManager#STANDBY_BUCKET_FREQUENT, android.app.usage.UsageStatsManager#STANDBY_BUCKET_RARE, android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED, or android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER

isAppInactive

Added in API level 23
fun isAppInactive(packageName: String!): Boolean

Returns whether the specified app is currently considered inactive. This will be true if the app hasn't been used directly or indirectly for a period of time defined by the system. This could be of the order of several hours or days. Apps are not considered inactive when the device is charging.

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS to query the inactive state of other apps

Parameters
packageName String!: The package name of the app to query
Return
Boolean whether the app is currently considered inactive or false if querying another app without android.Manifest.permission#PACKAGE_USAGE_STATS

queryAndAggregateUsageStats

Added in API level 21
fun queryAndAggregateUsageStats(
    beginTime: Long,
    endTime: Long
): MutableMap<String!, UsageStats!>!

A convenience method that queries for all stats in the given range (using the best interval for that range), merges the resulting data, and keys it by package name. See queryUsageStats(int,long,long).

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS

Parameters
beginTime Long: The inclusive beginning of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
MutableMap<String!, UsageStats!>! A java.util.Map keyed by package name

queryConfigurations

Added in API level 21
fun queryConfigurations(
    intervalType: Int,
    beginTime: Long,
    endTime: Long
): MutableList<ConfigurationStats!>!

Gets the hardware configurations the device was in for the given time range, aggregated by the specified interval. The results are ordered as in queryUsageStats(int,long,long).

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS

Note: Starting from Android R, if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.
Parameters
intervalType Int: The time interval by which the stats are aggregated.
beginTime Long: The inclusive beginning of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
MutableList<ConfigurationStats!>! A list of ConfigurationStats

queryEventStats

Added in API level 28
fun queryEventStats(
    intervalType: Int,
    beginTime: Long,
    endTime: Long
): MutableList<EventStats!>!

Gets aggregated event stats for the given time range, aggregated by the specified interval.

The returned list will contain a EventStats object for each event type that is being aggregated and has data for an interval that is a subset of the time range given.

The current event types that will be aggregated here are:

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS

Note: Starting from Android R, if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.
Parameters
intervalType Int: The time interval by which the stats are aggregated.
beginTime Long: The inclusive beginning of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
MutableList<EventStats!>! A list of EventStats

queryEvents

Added in API level 35
fun queryEvents(query: UsageEventsQuery): UsageEvents?

Query for events with specific UsageEventsQuery object. Note: if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.
Requires android.Manifest.permission#PACKAGE_USAGE_STATS

Parameters
query UsageEventsQuery: The query object used to specify the query parameters. This value cannot be null.
Return
UsageEvents? A UsageEvents which contains the events matching the query parameters.

queryEvents

Added in API level 21
fun queryEvents(
    beginTime: Long,
    endTime: Long
): UsageEvents!

Query for events in the given time range. Events are only kept by the system for a few days.

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS

Note: Starting from Android R, if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.
Parameters
beginTime Long: The inclusive beginning of the range of events to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of events to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
UsageEvents! A UsageEvents.

queryEventsForSelf

Added in API level 28
fun queryEventsForSelf(
    beginTime: Long,
    endTime: Long
): UsageEvents!

Like queryEvents(long,long), but only returns events for the calling package. Note: Starting from Android R, if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.

Parameters
beginTime Long: The inclusive beginning of the range of events to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of events to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
UsageEvents! A UsageEvents object.

queryUsageStats

Added in API level 21
fun queryUsageStats(
    intervalType: Int,
    beginTime: Long,
    endTime: Long
): MutableList<UsageStats!>!

Gets application usage stats for the given time range, aggregated by the specified interval.

The returned list will contain one or more UsageStats objects for each package, with usage data that covers at least the given time range. Note: The begin and end times of the time range may be expanded to the nearest whole interval period.

The caller must have android.Manifest.permission#PACKAGE_USAGE_STATS

Note: Starting from Android R, if the user's device is not in an unlocked state (as defined by UserManager.isUserUnlocked()), then null will be returned.
Parameters
intervalType Int: The time interval by which the stats are aggregated.
beginTime Long: The inclusive beginning of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
endTime Long: The exclusive end of the range of stats to include in the results. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
MutableList<UsageStats!>! A list of UsageStats