README.md
1Android AppUsageStatistics Sample
2===================================
3
4A basic app showing how to use App usage statistics API to let users collect statistics related
5to usage of the applications.
6
7Introduction
8------------
9
10The [App usage statistics][1] API allows app developers to collect statistics related to usage of
11the applications. This API provides more detailed usage information than the deprecated
12[getRecentTasks()][2] method.
13
14This example illustrates how to use the App usage statistics API by showing the applications sorted
15by the timestamp of the last time each app was used.
16
17To use this API, you must first declare the `android.permission.PACKAGE_USAGE_STATS` permission
18in your manifest. The user must also enable access for this app through
19`Settings > Security > Apps with usage access`.
20
21To collect the statistics of the app usage, you need to first get the instance of
22[UsageStatsManager][3] by the following code:
23
24```java
25mUsageStatsManager = (UsageStatsManager) getActivity()
26 .getSystemService(Context.USAGE_STATS_SERVICE);
27```
28
29Then you can retrieve the statistics of the app usage by the following method:
30
31```java
32Calendar cal = Calendar.getInstance();
33cal.add(Calendar.YEAR, -1);
34List<UsageStats> queryUsageStats = mUsageStatsManager
35 .queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(),
36 System.currentTimeMillis());
37```
38
39The first argument of the [queryUsageStats()][4] is used for the time interval by which the
40stats are aggregated. The second and the third arguments are used for specifying the beginning
41and the end of the range of the stats to include in the results.
42
43[1]: https://developer.android.com/reference/android/app/usage/package-summary.html
44[2]: https://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int%2C%20int)
45[3]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html
46[4]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int%2C%20long%2C%20long)
47
48Pre-requisites
49--------------
50
51- Android SDK v21
52- Android Build Tools v21.1.1
53- Android Support Repository
54
55Screenshots
56-------------
57
58<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> <img src="screenshots/screenshot-2.png" height="400" alt="Screenshot"/>
59
60Getting Started
61---------------
62
63This sample uses the Gradle build system. To build this project, use the
64"gradlew build" command or use "Import Project" in Android Studio.
65
66Support
67-------
68
69- Google+ Community: https://plus.google.com/communities/105153134372062985968
70- Stack Overflow: http://stackoverflow.com/questions/tagged/android
71
72If you've found an error in this sample, please file an issue:
73https://github.com/googlesamples/android-AppUsageStatistics
74
75Patches are encouraged, and may be submitted by forking this project and
76submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
77
78License
79-------
80
81Copyright 2014 The Android Open Source Project, Inc.
82
83Licensed to the Apache Software Foundation (ASF) under one or more contributor
84license agreements. See the NOTICE file distributed with this work for
85additional information regarding copyright ownership. The ASF licenses this
86file to you under the Apache License, Version 2.0 (the "License"); you may not
87use this file except in compliance with the License. You may obtain a copy of
88the License at
89
90http://www.apache.org/licenses/LICENSE-2.0
91
92Unless required by applicable law or agreed to in writing, software
93distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
94WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
95License for the specific language governing permissions and limitations under
96the License.
97