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