AppUsageStatistics
System
com.example.android.appusagestatistics
21
com.android.support:recyclerview-v7:+
com.android.support:appcompat-v7:21.+
PUBLISHED
System
Android
Java
Mobile
INTERMEDIATE
screenshots/web-icon.png
screenshots/screenshot-1.png
screenshots/screenshot-2.png
android.app.usage.UsageStats
android.app.usage.UsageStatsManager
Security > Apps with usage access`.
To collect the statistics of the app usage, you need to first get the instance of
[UsageStatsManager][3] by the following code:
```java
mUsageStatsManager = (UsageStatsManager) getActivity()
.getSystemService(Context.USAGE_STATS_SERVICE);
```
Then you can retrieve the statistics of the app usage by the following method:
```java
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
List queryUsageStats = mUsageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(),
System.currentTimeMillis());
```
The first argument of the [queryUsageStats()][4] is used for the time interval by which the
stats are aggregated. The second and the third arguments are used for specifying the beginning
and the end of the range of the stats to include in the results.
[1]: https://developer.android.com/reference/android/app/usage/package-summary.html
[2]: https://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int%2C%20int)
[3]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html
[4]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int%2C%20long%2C%20long)
]]>