概述
本示例主要展示了设备使用信息情况。
样例展示

基础信息

设备使用信息统计
介绍
本示例使用[bundleState]相关接口完成设备中应用时长的统计功能。
效果预览

使用说明:
1.顶部的数据面板展示了最常用的五个应用的使用时间占比情况。
2.中部的竖向滑动栏展示了每个应用的使用总时长和最后一次使用的时间。
3.底部的横向滑动栏展示了不常用应用列表。
具体实现
●该示例使用bundleState接口中isIdleState方法判断指定bundleName的应用当前是否是空闲状态来分类不常用应用列表,queryBundleStateInfos方法通过指定起始和结束时间查询应用使用时长统计信息来获得每个应用的使用总时长。
●源码链接:[BundleStateUtil.ets]
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import usageStatistics from '@ohos.resourceschedule.usageStatistics'
import AppTime from '../model/AppTime'
import Logger from './Logger'
import { AppInfo, appInit } from '../model/AppInfo'
import { getAppName, getAppIcon, getTimStr } from './Util'
const TAG: string = 'BundleStateUtil'
const BEGIN_HOURS: number = 0
const BEGIN_MINUTES: number = 0
const BEGIN_SECONDS: number = 0
const END_HOURS: number = 23
const END_MINUTES: number = 59
const END_SECONDS: number = 59
export class BundleStateUtil {
private apps: Array<AppInfo> = appInit()
private freeApps: Array<AppInfo> = []
/**
* 获取不常用应用列表
*/
async getFreeAppList(): Promise<Array<AppInfo>> {
for (let appsKey in this.apps) {
let bundleName = this.apps[appsKey].bundleName
let isIdleState = await usageStatistics.isIdleState(bundleName)
if (isIdleState) {
this.freeApps.push({ bundleName: bundleName, name: getAppName(bundleName), icon: getAppIcon(bundleName) })
}
Logger.info(TAG, `freeApps=${JSON.stringify(this.freeApps)}`)
}
return this.freeApps
}
/**
* 获取所有应用使用时间列表
*/

最低0.47元/天 解锁文章
576

被折叠的 条评论
为什么被折叠?



