跳过内容

《开始使用》

GitHub

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

终端窗口
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-usagestatsmanager
bunx cap sync
import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

根据给定时间范围查询和聚合使用统计数据。

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

检查是否已获得使用统计权限。

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

打开使用统计设置屏幕。 此操作将打开使用统计设置屏幕,允许用户授予使用统计权限。 即使权限已被授予,此操作也将始终打开设置屏幕。

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

查询设备上安装的所有包。需要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}`);
});

查询使用统计信息的选项。

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

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

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

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

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在上游发生变化时,请重新同步。

如果您正在使用 开始 来规划仪表板和API操作,连接它与 使用@capgo/capacitor-android-usagestatsmanager 为native能力在使用@capgo/capacitor-android-usagestatsmanager API概述 为实现细节在API概述 介绍 为实现细节在介绍 API密钥 为实现细节在API密钥,并且 设备 了解设备的实现细节。