跳过内容

开始使用

GitHub

您可以使用我们的AI辅助设置来安装插件。使用以下命令将Capgo技能添加到您的AI工具中。

Terminal window
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-android-usagestatsmanager` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

Terminal window
bun add @capgo/capacitor-android-usagestatsmanager
bunx cap sync

导入

导入
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

API 概述

API 概述

queryAndAggregateUsageStats

查询和聚合使用统计

查询和聚合使用统计

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

isUsageStatsPermissionGranted

查询和聚合使用统计

检查使用统计权限是否已授权

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

openUsageStatsSettings

开启使用统计设置

打开使用统计设置屏幕。

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

queryAllPackages

复制到剪贴板

Section titled “queryAllPackages”

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

查询设备上安装的所有包。

需要 QUERY_ALL_PACKAGES 权限。

UsageStatsOptions

类型参考

Section titled “Type Reference”

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;
}

Android 应用程序的使用统计。

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

使用统计权限结果

使用统计权限检查的结果。

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

PackageInfo

包信息

包信息表示已安装包的基本信息。

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;
}

真实数据来源

真实数据来源

本页面是由插件的 src/definitions.ts重新同步公共 API 时,重新运行同步。

继续从 Getting Started

标题:继续从 Getting Started

如果您正在使用 开始使用 来规划仪表板和 API 操作,请将其与 使用 @capgo/capacitor-android-usagestatsmanager 为 native 能力在使用 @capgo/capacitor-android-usagestatsmanager, API 概述 为实现细节在 API 概述, 介绍 为实现细节在介绍, API 键值 为 API 的实现细节 设备 为 __CAPGO_KEEP_0__ 的实现细节