openharmony 鸿蒙 js-apis-deviceUsageStatistics-sys

2025-06-12 浏览 (1)

@ohos.bundleState (Device Usage Statistics) (System API)

This module provides APIs for collecting statistics on device usage.

System applications can call these APIs to implement the following features:

  • Query the usage duration in different time segments, events (foreground, background, start and end of continuous tasks), and the number of notifications, on a per application basis.
  • Query the bundle group information of the invoking application itself.
  • Query the idle status of applications, including the invoking application itself.

NOTE

This module is deprecated since API version 9. You are advised to use @ohos.resourceschedule.usageStatistics (Device Usage Statistics) (System API) instead.

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.bundleState.

Modules to Import

import bundleState from '@ohos.bundleState'

bundleState.queryBundleStateInfos

queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void

Queries the application usage duration statistics based on the specified start time and end time. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.
callbackAsyncCallback<BundleActiveInfoResponse>YesCallback used to return the application usage duration statistics.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleStateInfos(0, 20000000000000, (err: BusinessError ,
  res: bundleState.BundleActiveInfoResponse ) => {
  if (err) {
    console.error('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
  } else {
    console.log('BUNDLE_ACTIVE queryBundleStateInfos callback success.');
    console.log('BUNDLE_ACTIVE queryBundleStateInfos callback result ' + JSON.stringify(res));
  }
});

bundleState.queryBundleStateInfos

queryBundleStateInfos(begin: number, end: number): Promise<BundleActiveInfoResponse>

Queries the application usage duration statistics based on the specified start time and end time. This API uses a promise to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.

Return value

TypeDescription
Promise<BundleActiveInfoResponse>Promise used to return the result. return the application usage duration statistics.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleStateInfos(0, 20000000000000).then((res: bundleState.BundleActiveInfoResponse) => {
  console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.');
  console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res));
}).catch((err: BusinessError) => {
  console.error('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code);
});

bundleState.queryBundleStateInfoByInterval

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void

Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
byIntervalIntervalTypeYesType of information to be queried.
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.
callbackAsyncCallback<Array<BundleStateInfo>>YesCallback used to return the application usage duration statistics.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000, (err: BusinessError, res: Array<bundleState.BundleStateInfo>) => {
  if (err) {
    console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code);
  } else {
    console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback success.');
    for (let i = 0; i < res.length; i++) {
      console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback number : ' + (i + 1));
      console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback result ' + JSON.stringify(res[i]));
    }
  }
});

bundleState.queryBundleStateInfoByInterval

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number): Promise<Array<BundleStateInfo>>

Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually). This API uses a promise to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
byIntervalIntervalTypeYesType of information to be queried.
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.

Return value

TypeDescription
Promise<Array<BundleStateInfo>>Promise used to return the application usage duration statistics.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000).then((res: Array<bundleState.BundleStateInfo>) => {
  console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.');
  for (let i = 0; i < res.length; i++) {
    console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise number : ' + (i + 1));
    console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise result ' + JSON.stringify(res[i]));
  }
}).catch((err: BusinessError) => {
  console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code);
});

bundleState.queryBundleActiveStates

queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

Queries events of all applications based on the specified start time and end time. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.
callbackAsyncCallback<Array<BundleActiveState>>YesCallback used to return the events obtained.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleActiveStates(0, 20000000000000, (err: BusinessError, res: Array<bundleState.BundleActiveState>) => {
  if (err) {
    console.error('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
  } else {
    console.log('BUNDLE_ACTIVE queryBundleActiveStates callback success.');
    for (let i = 0; i < res.length; i++) {
      console.log('BUNDLE_ACTIVE queryBundleActiveStates callback number : ' + (i + 1));
      console.log('BUNDLE_ACTIVE queryBundleActiveStates callback result ' + JSON.stringify(res[i]));
    }
  }
});

bundleState.queryBundleActiveStates

queryBundleActiveStates(begin: number, end: number): Promise<Array<BundleActiveState>>

Queries events of all applications based on the specified start time and end time. This API uses a promise to return the result.

Required permissions: ohos.permission.BUNDLE_ACTIVE_INFO

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.

Return value

TypeDescription
Promise<Array<BundleActiveState>>Promise used to return the result. return the events obtained.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryBundleActiveStates(0, 20000000000000).then((res: Array<bundleState.BundleActiveState>) => {
  console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.');
  for (let i = 0; i < res.length; i++) {
    console.log('BUNDLE_ACTIVE queryBundleActiveStates promise number : ' + (i + 1));
    console.log('BUNDLE_ACTIVE queryBundleActiveStates promise result ' + JSON.stringify(res[i]));
  }
}).catch((err: BusinessError) => {
  console.error('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code);
});

bundleState.queryAppUsagePriorityGroup

queryAppUsagePriorityGroup(): Promise<number>

Queries the priority group of this application. This API uses a promise to return the result.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.AppGroup

System API: This is a system API.

Return value

TypeDescription
Promise<number>Promise used to return the priority group.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryAppUsagePriorityGroup().then((res: number) => {
  console.log('BUNDLE_ACTIVE QueryPackageGroup promise succeeded. result: ' + JSON.stringify(res));
}).catch((err: BusinessError) => {
  console.error('BUNDLE_ACTIVE QueryPackageGroup promise failed. because: ' + err.code);
});

bundleState.queryAppUsagePriorityGroup

queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void

Queries the priority group of this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.AppGroup

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the priority group.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryAppUsagePriorityGroup((err: BusinessError, res: number) => {
  if(err) {
    console.error('BUNDLE_ACTIVE QueryPackageGroup callback failed. because: ' + err.code);
  } else {
    console.log('BUNDLE_ACTIVE QueryPackageGroup callback succeeded. result: ' + JSON.stringify(res));
  }
});

bundleState.queryCurrentBundleActiveStates

queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

Queries events of this application based on the specified start time and end time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.
callbackAsyncCallback<Array<BundleActiveState>>YesCallback used to return the events.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryCurrentBundleActiveStates(0, 20000000000000, (err: BusinessError, res: Array<bundleState.BundleActiveState>) => {
  if (err) {
    console.error('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code);
  } else {
    console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback success.');
    for (let i = 0; i < res.length; i++) {
      console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback number : ' + (i + 1));
      console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback result ' + JSON.stringify(res[i]));
    }
  }
});

bundleState.queryCurrentBundleActiveStates

queryCurrentBundleActiveStates(begin: number, end: number): Promise<Array<BundleActiveState>>

Queries events of this application based on the specified start time and end time. This API uses a promise to return the result.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
beginnumberYesStart time, in milliseconds.
endnumberYesEnd time, in milliseconds.

Return value

TypeDescription
Promise<Array<BundleActiveState>>Promise used to return the events.

Example

import { BusinessError } from '@ohos.base';

bundleState.queryCurrentBundleActiveStates(0, 20000000000000).then((res: Array<bundleState.BundleActiveState>) => {
  console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise success.');
  for (let i = 0; i < res.length; i++) {
    console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise number : ' + (i + 1));
    console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise result ' + JSON.stringify(res[i]));
  }
}).catch((err: BusinessError) => {
  console.error('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code);
});

BundleStateInfo

Provides the usage duration information of an application.

Properties

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

NameTypeMandatoryDescription
bundleNamestringNoBundle name of the application.
abilityPrevAccessTimenumberNoLast time when the application was used.
abilityInFgTotalTimenumberNoTotal time that the application runs in the foreground.
idnumberYesUser ID.
abilityPrevSeenTimenumberNoLast time when the application was visible in the foreground.
abilitySeenTotalTimenumberNoTotal time that the application is visible in the foreground.
fgAbilityAccessTotalTimenumberNoTotal time that the application accesses the foreground.
fgAbilityPrevAccessTimenumberNoLast time when the application accessed the foreground.
infosBeginTimenumberNoTime logged in the first application usage record in the BundleActiveInfo object.
infosEndTimenumberNoTime logged in the last application usage record in the BundleActiveInfo object.

merge(deprecated)

merge(toMerge: BundleStateInfo): void

Merges the device usage statistics of applications with the same bundle name.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

Parameters

NameTypeMandatoryDescription
toMergeBundleStateInfoYesDevice usage statistics to merge.

BundleActiveState

Provides information about an application event.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

NameTypeMandatoryDescription
bundleNamestringNoBundle name of the application.
stateTypenumberNoApplication event type.
stateOccurredTimenumberNoTimestamp when the application event occurs.
appUsagePriorityGroupnumberNoGroup of the application by usage priority.
indexOfLinkstringNoShortcut ID.
nameOfClassstringNoClass name.

BundleActiveInfoResponse

Provides the usage duration information of an application.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

NameTypeMandatoryDescription
[key: string]: BundleStateInfo[key: string]: BundleStateInfoYesUsage duration information by application.

IntervalType

Enumerates the interval types for querying the application usage duration.

System capability: SystemCapability.ResourceSchedule.UsageStatistics.App

NameValueDescription
BY_OPTIMIZED0The system obtains the application usage duration statistics in the specified time frame at the interval the system deems appropriate.
BY_DAILY1The system queries the application usage duration statistics in the specified time frame on a daily basis.
BY_WEEKLY2The system queries the application usage duration statistics in the specified time frame on a weekly basis.
BY_MONTHLY3The system queries the application usage duration statistics in the specified time frame on a monthly basis.
BY_ANNUALLY4The system queries the application usage duration statistics in the specified time frame on an annual basis.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Background Tasks Kit

harmony 鸿蒙BackgroundProcessManager

harmony 鸿蒙TransientTask

harmony 鸿蒙TransientTask_DelaySuspendInfo

harmony 鸿蒙background_process_manager.h

harmony 鸿蒙DeviceUsageStatistics Error Codes

harmony 鸿蒙backgroundTaskManager Error Codes

harmony 鸿蒙reminderAgentManager Error Codes

harmony 鸿蒙workScheduler Error Codes

harmony 鸿蒙@ohos.WorkSchedulerExtensionAbility (Deferred Task Scheduling Callbacks)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/9w3Cj1