Passer au contenu

Getting Started

Ce contenu n'est pas encore disponible dans votre langue.

Terminal window
bun add @capgo/capacitor-android-usagestatsmanager
bunx cap sync
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

Queries and aggregates usage stats for the given time range.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;
const now = Date.now();
const stats = await UsageStatsManager.queryAndAggregateUsageStats({
beginTime: oneDayAgo,
endTime: now
});
for (const [packageName, usageData] of Object.entries(stats)) {
console.log(`${packageName}: ${usageData.totalTimeInForeground}ms`);
}

Checks if the usage stats permission is granted.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
const { granted } = await UsageStatsManager.isUsageStatsPermissionGranted();
if (!granted) {
await UsageStatsManager.openUsageStatsSettings();
}

Open the usage stats settings screen. This will open the usage stats settings screen, which allows the user to grant the usage stats permission. This will always open the settings screen, even if the permission is already granted.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
await UsageStatsManager.openUsageStatsSettings();

Queries all installed packages on the device. Requires the QUERY_ALL_PACKAGES permission.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
const { packages } = await UsageStatsManager.queryAllPackages();
packages.forEach(pkg => {
console.log(`${pkg.appName} (${pkg.packageName}): v${pkg.versionName}`);
});

Options for querying usage statistics.

export interface UsageStatsOptions {
/**
* The inclusive beginning of the range of stats to include in the results.
* Defined in terms of "Unix time"
*/
beginTime: number;
/**
* The exclusive end of the range of stats to include in the results.
* Defined in terms of "Unix time"
*/
endTime: number;
}

Usage statistics for an Android app.

export interface UsageStats {
/**
* The first timestamp of the usage stats.
*/
firstTimeStamp: number;
/**
* The last timestamp of the usage stats.
*/
lastTimeStamp: number;
/**
* Only available on Android Q (API level 29) and above.
* Will be undefined on lower Android versions.
*/
lastTimeForegroundServiceUsed?: number;
/**
* The last time the app was used.
*/
lastTimeUsed: number;
/**
* Only available on Android Q (API level 29) and above.
* Will be undefined on lower Android versions.
*/
lastTimeVisible?: number;
/**
* The name of the package.
*/
packageName: string;
/**
* Only available on Android Q (API level 29) and above.
* Will be undefined on lower Android versions.
*/
totalForegroundServiceUsed?: number;
/**
* The total time the app was in the foreground.
*/
totalTimeInForeground: number;
/**
* Only available on Android Q (API level 29) and above.
* Will be undefined on lower Android versions.
*/
totalTimeVisible?: number;
}

Result of a usage stats permission check.

export interface UsageStatsPermissionResult {
/**
* Whether the usage stats permission is granted.
*/
granted: boolean;
}

Represents basic information about an installed package.

export interface PackageInfo {
/** Package name */
packageName: string;
/** App display name */
appName: string;
/** Version name string */
versionName: string;
/** Version code number */
versionCode: number;
/** First install time in milliseconds since epoch */
firstInstallTime: number;
/** Last update time in milliseconds since epoch */
lastUpdateTime: number;
}

This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.