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