Skip to content

Latest commit

 

History

History
 
 

app_usage

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

app_usage

pub package

Install

Add app_usage as a dependency in pubspec.yaml. For help on adding as a dependency, view the documentation.

Android

NB: Requires API level 21 as a minimum!

You need to add the following package to the manifest namespace in AndroidManifest.xml:

xmlns:tools="http://schemas.android.com/tools"

as well as the following permissions to the manifest:

    <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions" />

Below is an example of how the start of your manifest should look in the end

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR_PACKAGE_NAME_HERE"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions"/>

Usage

void getUsageStats() async {
    // Initialization
    AppUsage appUsage = new AppUsage();
    try {
      // Define a time interval
      DateTime endDate = new DateTime.now();
      DateTime startDate = DateTime(endDate.year, endDate.month, endDate.day, 0, 0, 0);
      
      // Fetch the usage stats
      Map<String, double> usage = await appUsage.fetchUsage(startDate, endDate);
      
      // (Optional) Remove entries for apps with 0 usage time
      usage.removeWhere((key,val) => val == 0);
    }
    on AppUsageException catch (exception) {
      print(exception);
    }
}

Example

The first screen will ask for permission to view usage stats. Tap on your application. Screen 1

Tap on the 'Permit usage access' switch. Screen 2

Your application is now running, if the manifest has been corectly set up you should be presented with the following screen after tapping the refresh button. Screen 3