开始使用
复制一个包含安装步骤和此插件的完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-android-usagestatsmanager`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/android-usagestatsmanager/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
标题为“安装”您可以使用我们的 AI 助手来安装插件。将以下命令添加到您的 AI 工具中:Capgo
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-android-usagestatsmanager` plugin in my project.如果您更喜欢手动设置,请运行以下命令安装插件并按照以下平台特定的说明:
bun add @capgo/capacitor-android-usagestatsmanagerbunx cap sync导入
标题:导入import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';API概述
标题:API概述queryAndAggregateUsageStats
标题:queryAndAggregateUsageStats为给定时间范围查询和聚合使用统计信息。
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`);}isUsageStatsPermissionGranted
Section titled “isUsageStatsPermissionGranted”检查是否已获得使用统计权限
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
const { granted } = await UsageStatsManager.isUsageStatsPermissionGranted();if (!granted) { await UsageStatsManager.openUsageStatsSettings();}openUsageStatsSettings
Section titled “openUsageStatsSettings”打开使用统计设置屏幕。 这将打开使用统计设置屏幕,允许用户授予使用统计权限。 这将始终打开设置屏幕,即使权限已被授予。
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
await UsageStatsManager.openUsageStatsSettings();queryAllPackages
Section titled “queryAllPackages”查询设备上安装的所有包。 需要 QUERY_ALL_PACKAGES 权限。
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';
const { packages } = await UsageStatsManager.queryAllPackages();packages.forEach(pkg => { console.log(`${pkg.appName} (${pkg.packageName}): v${pkg.versionName}`);});UsageStatsOptions
Section titled “UsageStatsOptions”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;}UsageStats
Section titled “UsageStats”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;}UsageStatsPermissionResult
Section titled “UsageStatsPermissionResult”Result of a usage stats permission check.
export interface UsageStatsPermissionResult { /** * Whether the usage stats permission is granted. */ granted: boolean;}PackageInfo
Section titled “PackageInfo”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;}Source Of Truth
数据源本页面由插件生成 src/definitions.ts当公共资源API发生更改时,请重新同步
继续从Getting Started
继续从Getting Started如果您正在使用 Getting Started 来规划仪表板和API操作,请将其与 使用@capgo/capacitor-android-usagestatsmanager 为native能力在使用@capgo/capacitor-android-usagestatsmanager API概述 查看API概述 简介 简介中的实现细节 API Keys API Keys中的实现细节 设备 设备中的实现细节