UsageStatsManager
public
final
class
UsageStatsManager
extends Object
java.lang.Object | |
↳ | 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 \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014||\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014¦-----------------------| YEAR || ¦ | \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014||\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014¦-----------------------| MONTH | || MONTH ¦ | \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014|\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014||\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014¦-----------------------| | WEEK | WEEK|| | WEEK | WE¦EK | WEEK | \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014||\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014|\u2014\u2014\u2014\u2014\u2014\u2014\u2014¦-----------------------| || |DAY|DAY|DAY|DAY¦DAY|DAY|DAY|DAY|DAY|DAY| \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014||\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014¦-----------------------|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
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 | |
---|---|
String |
EXTRA_EVENT_ACTION
A String extra, when used with |
String |
EXTRA_EVENT_CATEGORY
A String extra, when used with |
int |
INTERVAL_BEST
An interval type that will use the best fit interval for the given time range. |
int |
INTERVAL_DAILY
An interval type that spans a day. |
int |
INTERVAL_MONTHLY
An interval type that spans a month. |
int |
INTERVAL_WEEKLY
An interval type that spans a week. |
int |
INTERVAL_YEARLY
An interval type that spans a year. |
int |
STANDBY_BUCKET_ACTIVE
The app was used very recently, currently in use or likely to be used very soon. |
int |
STANDBY_BUCKET_FREQUENT
The app was used in the last few days and/or likely to be used in the next few days. |
int |
STANDBY_BUCKET_RARE
The app has not be used for several days and/or is unlikely to be used for several days. |
int |
STANDBY_BUCKET_RESTRICTED
The app has not be used for several days, is unlikely to be used for several days, and has been misbehaving in some manner. |
int |
STANDBY_BUCKET_WORKING_SET
The app was used recently and/or likely to be used in the next few hours. |
Public methods | |
---|---|
int
|
getAppStandbyBucket()
Returns the current standby bucket of the calling app. |
boolean
|
isAppInactive(String packageName)
Returns whether the specified app is currently considered inactive. |
Map<String, UsageStats>
|
queryAndAggregateUsageStats(long beginTime, long endTime)
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. |
List<ConfigurationStats>
|
queryConfigurations(int intervalType, long beginTime, long endTime)
Gets the hardware configurations the device was in for the given time range, aggregated by the specified interval. |
List<EventStats>
|
queryEventStats(int intervalType, long beginTime, long endTime)
Gets aggregated event stats for the given time range, aggregated by the specified interval. |
UsageEvents
|
queryEvents(long beginTime, long endTime)
Query for events in the given time range. |
UsageEvents
|
queryEvents(UsageEventsQuery query)
Query for events with specific UsageEventsQuery object. |
UsageEvents
|
queryEventsForSelf(long beginTime, long endTime)
Like |
List<UsageStats>
|
queryUsageStats(int intervalType, long beginTime, long endTime)
Gets application usage stats for the given time range, aggregated by the specified interval. |
Inherited methods | |
---|---|
Constants
EXTRA_EVENT_ACTION
public static final String EXTRA_EVENT_ACTION
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.
Constant Value: "android.app.usage.extra.EVENT_ACTION"
EXTRA_EVENT_CATEGORY
public static final String EXTRA_EVENT_CATEGORY
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.
Constant Value: "android.app.usage.extra.EVENT_CATEGORY"
INTERVAL_BEST
public static final int INTERVAL_BEST
An interval type that will use the best fit interval for the given time range.
See queryUsageStats(int, long, long)
.
Constant Value: 4 (0x00000004)
INTERVAL_DAILY
public static final int INTERVAL_DAILY
An interval type that spans a day. See queryUsageStats(int, long, long)
.
Constant Value: 0 (0x00000000)
INTERVAL_MONTHLY
public static final int INTERVAL_MONTHLY
An interval type that spans a month. See queryUsageStats(int, long, long)
.
Constant Value: 2 (0x00000002)
INTERVAL_WEEKLY
public static final int INTERVAL_WEEKLY
An interval type that spans a week. See queryUsageStats(int, long, long)
.
Constant Value: 1 (0x00000001)
INTERVAL_YEARLY
public static final int INTERVAL_YEARLY
An interval type that spans a year. See queryUsageStats(int, long, long)
.
Constant Value: 3 (0x00000003)
STANDBY_BUCKET_ACTIVE
public static final int STANDBY_BUCKET_ACTIVE
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.
See also:
Constant Value: 10 (0x0000000a)
STANDBY_BUCKET_FREQUENT
public static final int STANDBY_BUCKET_FREQUENT
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.
See also:
Constant Value: 30 (0x0000001e)
STANDBY_BUCKET_RARE
public static final int STANDBY_BUCKET_RARE
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.
See also:
Constant Value: 40 (0x00000028)
STANDBY_BUCKET_RESTRICTED
public static final int STANDBY_BUCKET_RESTRICTED
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
.
See also:
Constant Value: 45 (0x0000002d)
STANDBY_BUCKET_WORKING_SET
public static final int STANDBY_BUCKET_WORKING_SET
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.
See also:
Constant Value: 20 (0x00000014)
Public methods
getAppStandbyBucket
public int getAppStandbyBucket ()
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
.
Returns | |
---|---|
int |
the current standby bucket of the calling app. One of STANDBY_BUCKET_* constants.
Value is android.app.usage.UsageStatsManager.STANDBY_BUCKET_EXEMPTED, STANDBY_BUCKET_ACTIVE , STANDBY_BUCKET_WORKING_SET , STANDBY_BUCKET_FREQUENT , STANDBY_BUCKET_RARE , STANDBY_BUCKET_RESTRICTED , or android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER |
isAppInactive
public boolean isAppInactive (String packageName)
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 Manifest.permission.PACKAGE_USAGE_STATS
to query the
inactive state of other apps
Parameters | |
---|---|
packageName |
String : The package name of the app to query |
Returns | |
---|---|
boolean |
whether the app is currently considered inactive or false if querying another app
without Manifest.permission.PACKAGE_USAGE_STATS |
queryAndAggregateUsageStats
public Map<String, UsageStats> queryAndAggregateUsageStats (long beginTime, long endTime)
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 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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of stats to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
Map<String, UsageStats> |
A Map keyed by package name |
queryConfigurations
public List<ConfigurationStats> queryConfigurations (int intervalType, long beginTime, long endTime)
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 Manifest.permission.PACKAGE_USAGE_STATS
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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of stats to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
List<ConfigurationStats> |
A list of ConfigurationStats |
queryEventStats
public List<EventStats> queryEventStats (int intervalType, long beginTime, long endTime)
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:
UsageEvents.Event#SCREEN_INTERACTIVE
UsageEvents.Event#SCREEN_NON_INTERACTIVE
UsageEvents.Event#KEYGUARD_SHOWN
UsageEvents.Event#KEYGUARD_HIDDEN
The caller must have Manifest.permission.PACKAGE_USAGE_STATS
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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of stats to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
List<EventStats> |
A list of EventStats |
queryEvents
public UsageEvents queryEvents (long beginTime, long endTime)
Query for events in the given time range. Events are only kept by the system for a few days.
The caller must have Manifest.permission.PACKAGE_USAGE_STATS
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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of events to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
UsageEvents |
A UsageEvents . |
queryEvents
public UsageEvents queryEvents (UsageEventsQuery query)
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 Manifest.permission.PACKAGE_USAGE_STATS
Parameters | |
---|---|
query |
UsageEventsQuery : The query object used to specify the query parameters.
This value cannot be null . |
Returns | |
---|---|
UsageEvents |
A UsageEvents which contains the events matching the query parameters. |
queryEventsForSelf
public UsageEvents queryEventsForSelf (long beginTime, long endTime)
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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of events to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
UsageEvents |
A UsageEvents object. |
See also:
queryUsageStats
public List<UsageStats> queryUsageStats (int intervalType, long beginTime, long endTime)
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 Manifest.permission.PACKAGE_USAGE_STATS
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
System.currentTimeMillis() . |
endTime |
long : The exclusive end of the range of stats to include in the results. Defined
in terms of "Unix time", see System.currentTimeMillis() . |
Returns | |
---|---|
List<UsageStats> |
A list of UsageStats |