【鸿蒙开发实战案例】手机应用使用信息统计

概述

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

样例展示

在这里插入图片描述

基础信息

在这里插入图片描述

设备使用信息统计

介绍

本示例使用[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

  }



  /**

   * 获取所有应用使用时间列表

   */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值