public final class UsageStatsManager extends Object
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: This API requires the permission android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.
Modifier and Type | Field and Description |
---|---|
static int |
INTERVAL_BEST
An interval type that will use the best fit interval for the given time range.
|
static int |
INTERVAL_COUNT
The number of available intervals.
|
static int |
INTERVAL_DAILY
An interval type that spans a day.
|
static int |
INTERVAL_MONTHLY
An interval type that spans a month.
|
static int |
INTERVAL_WEEKLY
An interval type that spans a week.
|
static int |
INTERVAL_YEARLY
An interval type that spans a year.
|
Constructor and Description |
---|
UsageStatsManager(Context context,
IUsageStatsManager service) |
Modifier and Type | Method and Description |
---|---|
boolean |
isAppInactive(String packageName)
Returns whether the specified app is currently considered inactive.
|
void |
onCarrierPrivilegedAppsChanged()
Inform usage stats that the carrier privileged apps access rules have changed.
|
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.
|
UsageEvents |
queryEvents(long beginTime,
long endTime)
Query for events in the given time range.
|
List<UsageStats> |
queryUsageStats(int intervalType,
long beginTime,
long endTime)
Gets application usage stats for the given time range, aggregated by the specified interval.
|
void |
setAppInactive(String packageName,
boolean inactive) |
void |
whitelistAppTemporarily(String packageName,
long duration,
UserHandle user)
Temporarily whitelist the specified app for a short duration.
|
public static final int INTERVAL_DAILY
queryUsageStats(int, long, long)
.public static final int INTERVAL_WEEKLY
queryUsageStats(int, long, long)
.public static final int INTERVAL_MONTHLY
queryUsageStats(int, long, long)
.public static final int INTERVAL_YEARLY
queryUsageStats(int, long, long)
.public static final int INTERVAL_BEST
queryUsageStats(int, long, long)
.public static final int INTERVAL_COUNT
INTERVAL_BEST
, since it
is a pseudo interval (it actually selects a real interval).
public UsageStatsManager(Context context, IUsageStatsManager service)
public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime)
The returned list will contain a UsageStats
object for each package that
has data for an interval that is a subset of the time range given. To illustrate:
intervalType = INTERVAL_YEARLY beginTime = 2013 endTime = 2015 (exclusive) Results: 2013 - com.example.alpha 2013 - com.example.beta 2014 - com.example.alpha 2014 - com.example.beta 2014 - com.example.charlie
intervalType
- The time interval by which the stats are aggregated.beginTime
- The inclusive beginning of the range of stats to include in the results.endTime
- The exclusive end of the range of stats to include in the results.UsageStats
or null if none are available.INTERVAL_DAILY
,
INTERVAL_WEEKLY
,
INTERVAL_MONTHLY
,
INTERVAL_YEARLY
,
INTERVAL_BEST
public List<ConfigurationStats> queryConfigurations(int intervalType, long beginTime, long endTime)
queryUsageStats(int, long, long)
.intervalType
- The time interval by which the stats are aggregated.beginTime
- The inclusive beginning of the range of stats to include in the results.endTime
- The exclusive end of the range of stats to include in the results.ConfigurationStats
or null if none are available.public UsageEvents queryEvents(long beginTime, long endTime)
beginTime
- The inclusive beginning of the range of events to include in the results.endTime
- The exclusive end of the range of events to include in the results.UsageEvents
.public Map<String,UsageStats> queryAndAggregateUsageStats(long beginTime, long endTime)
queryUsageStats(int, long, long)
.beginTime
- The inclusive beginning of the range of stats to include in the results.endTime
- The exclusive end of the range of stats to include in the results.Map
keyed by package name, or null if no stats are
available.public boolean isAppInactive(String packageName)
packageName
- The package name of the app to querypublic void setAppInactive(String packageName, boolean inactive)
public void whitelistAppTemporarily(String packageName, long duration, UserHandle user)
packageName
- The package name of the app to whitelist.duration
- Duration to whitelist the app for, in milliseconds. It is recommended that
this be limited to 10s of seconds. Requested duration will be clamped to a few minutes.user
- The user for whom the package should be whitelisted. Passing in a user that is
not the same as the caller's process will require the INTERACT_ACROSS_USERS permission.isAppInactive(String)
public void onCarrierPrivilegedAppsChanged()