Skip to content
TheBotBox edited this page May 19, 2019 · 11 revisions

Welcome to the AppsUsageMonitorAPI wiki!
The goal of this library is to provide an easy API to access the usage stats of applications in an Android device.

Contents

Requirements
Implementation
API Usage

Requirements

  1. Minimum SDK Version - API Level 21
  2. Usage Permissions - As this library mostly works on top of UsageStatsManager, most methods of which require the permission android.permission.PACKAGE_USAGE_STATS

Implementation

Library is available on JCenter, simply add the following line in your app build.gradle

implementation 'the.bot.box:appusagemonitor:{latest-version}'

where {latest-version} corresponds to latest published version Download

API Usage

First and foremost,in order to fetch list of used applications, we need to secure USAGE ACCESS permission from the user. For that, declare below line in the application's Manifest.xml

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

& to check, if the permission is granted or not,call

          if (Monitor.hasUsagePermission())
        else
            Monitor.requestUsagePermission();

Fetch list of all application

Once, permission has been granted, one can easily fetch the list of application used by calling,

            Monitor.scan().getAppLists(this).fetchFor(Duration.TODAY);

where .getAppList(UsageContracts.View mView) returns a callback in your view once the list has been fetched.

further .fetchFor() requires the duration for which you want to query for usage details.
As of now, one can chose from Duration.TODAY, Duration.YESTERDAY, Duration.WEEK, Duration.MONTH

Final Implementation of the UsageLibrary may look like below:

    public class MainActivity extends AppCompatActivity implements UsageContracts.View{
    
     @Override
    protected void onResume() {
        super.onResume();
        if (Monitor.hasUsagePermission()) {
            Monitor.scan().getAppLists(this).fetchFor(Duration.TODAY);
            init();
        } else {
            Monitor.requestUsagePermission();
        }
    }
    
    @Override
    public void getUsageData(List<AppData> usageData, long mTotalUsage, int duration) {
      /**
       * 
       * @param usageData list of application that has been used within the duration for which query has been made.
       * @param mTotalUsage a sum total(in millis) of the usage by each and every app with in the request duration. 
       * @param the same duration for which query has been made i.e.fetchFor(Duration...)
       */
    }    
    
    @Override
    public void showProgress() {}

    @Override
    public void hideProgress() {}
}

mTotalUsage returns total usage time in milliSeconds. To convert into string format, use library's in-built function i.e. UsageUtils.humanReadableMillis(mTotalUsage)

List<AppData> usageData list of used apps with usage stats. For futher details on AppData model, please dive into AppAdapter.java

Have a look on final output:

Fetch Usage for a specific package

updating soon..