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