Home | History | Annotate | Download | only in AppUsageStatistics
      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <!--
      3  Copyright 2014 The Android Open Source Project
      4 
      5  Licensed under the Apache License, Version 2.0 (the "License");
      6  you may not use this file except in compliance with the License.
      7  You may obtain a copy of the License at
      8 
      9      http://www.apache.org/licenses/LICENSE-2.0
     10 
     11  Unless required by applicable law or agreed to in writing, software
     12  distributed under the License is distributed on an "AS IS" BASIS,
     13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  See the License for the specific language governing permissions and
     15  limitations under the License.
     16 -->
     17 <sample>
     18     <name>AppUsageStatistics</name>
     19     <group>System</group>
     20     <package>com.example.android.appusagestatistics</package>
     21 
     22     <minSdk>21</minSdk>
     23 
     24     <dependency>com.android.support:recyclerview-v7:+</dependency>
     25     <dependency>com.android.support:appcompat-v7:21.+</dependency>
     26 
     27     <strings>
     28         <intro>
     29             <![CDATA[
     30             This sample explains how to use App usage statistics API, which was introduced
     31             in Android 5.0.
     32             ]]>
     33         </intro>
     34     </strings>
     35 
     36     <template src="base" />
     37 
     38     <metadata>
     39         <status>PUBLISHED</status>
     40         <categories>System</categories>
     41         <technologies>Android</technologies>
     42         <languages>Java</languages>
     43         <solutions>Mobile</solutions>
     44         <level>INTERMEDIATE</level>
     45         <icon>screenshots/web-icon.png</icon>
     46         <screenshots>
     47             <img>screenshots/screenshot-1.png</img>
     48             <img>screenshots/screenshot-2.png</img>
     49         </screenshots>
     50         <api_refs>
     51             <android>android.app.usage.UsageStats</android>
     52             <android>android.app.usage.UsageStatsManager</android>
     53         </api_refs>
     54 
     55         <description>
     56 <![CDATA[
     57 A basic app showing how to use App usage statistics API to let users collect statistics related
     58 to usage of the applications.
     59 ]]>
     60         </description>
     61 
     62         <intro>
     63 <![CDATA[
     64 The [App usage statistics][1] API allows app developers to collect statistics related to usage of
     65 the applications. This API provides more detailed usage information than the deprecated
     66 [getRecentTasks()][2] method.
     67 
     68 This example illustrates how to use the App usage statistics API by showing the applications sorted
     69 by the timestamp of the last time each app was used.
     70 
     71 To use this API, you must first declare the `android.permission.PACKAGE_USAGE_STATS` permission
     72 in your manifest. The user must also enable access for this app through
     73 `Settings > Security > Apps with usage access`.
     74 
     75 To collect the statistics of the app usage, you need to first get the instance of
     76 [UsageStatsManager][3] by the following code:
     77 
     78 ```java
     79 mUsageStatsManager = (UsageStatsManager) getActivity()
     80        .getSystemService(Context.USAGE_STATS_SERVICE);
     81 ```
     82 
     83 Then you can retrieve the statistics of the app usage by the following method:
     84 
     85 ```java
     86 Calendar cal = Calendar.getInstance();
     87 cal.add(Calendar.YEAR, -1);
     88 List<UsageStats> queryUsageStats = mUsageStatsManager
     89         .queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(),
     90                 System.currentTimeMillis());
     91 ```
     92 
     93 The first argument of the [queryUsageStats()][4] is used for the time interval by which the
     94 stats are aggregated. The second and the third arguments are used for specifying the beginning
     95 and the end of the range of the stats to include in the results.
     96 
     97 [1]: https://developer.android.com/reference/android/app/usage/package-summary.html
     98 [2]: https://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int%2C%20int)
     99 [3]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html
    100 [4]: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int%2C%20long%2C%20long)
    101 ]]>
    102         </intro>
    103     </metadata>
    104 </sample>
    105