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