当前位置: 首页 > article >正文

鸿蒙NEXT开发App相关工具类

import bundleManager from '@ohos.bundle.bundleManager';
import { KeyboardAvoidMode, window } from '@kit.ArkUI';
import { common, ConfigurationConstant } from '@kit.AbilityKit';/*** App相关工具类(使用该工具前请在UIAbility的onWindowStageCreate方法中调用AppUtil的init方法初始化)* author: 鸿蒙布道师* since: 2024/03/25*/
export class AppUtil {private static context: common.UIAbilityContext; // 上下文/*** 初始化方法,缓存全局变量,在UIAbility的onCreate方法中初始化该方法。* @param context UIAbility上下文*/static init(context: common.UIAbilityContext) {AppUtil.context = context;}/*** 获取上下文,common.UIAbilityContext* @returns UIAbility上下文*/static getContext(): common.UIAbilityContext {if (!AppUtil.context) {throw new Error("请在UIAbility的onCreate方法中调用AppUtil的init方法初始化!");}return AppUtil.context;}/*** 获取WindowStage* @returns WindowStage*/static getWindowStage(): window.WindowStage {return AppUtil.getContext().windowStage;}/*** 获取主窗口* @returns 主窗口*/static getMainWindow(): window.Window {return AppUtil.getWindowStage().getMainWindowSync();}/*** 获取UIContext* @returns UIContext*/static getUIContext(): UIContext {return AppUtil.getMainWindow().getUIContext();}/*** 设置灰阶,APP一键置灰。* @param grayScale 该参数为浮点数,取值范围为[0.0, 1.0]。* @param onlyMainWindow 是否只置灰主窗口,默认false。*/static async setGrayScale(grayScale: number = 1.0, onlyMainWindow: boolean = false): Promise<void> {AppUtil.getMainWindow().setWindowGrayScale(grayScale);if (!onlyMainWindow) {const subWindows = await AppUtil.getWindowStage().getSubWindow();subWindows?.forEach((subWindow) => subWindow.setWindowGrayScale(grayScale));}}/*** 设置应用的颜色模式。仅支持主线程调用。* @param colorMode 颜色模式*/static setColorMode(colorMode: ConfigurationConstant.ColorMode = ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET) {AppUtil.getContext().getApplicationContext().setColorMode(colorMode);}/*** 设置应用的字体类型。仅支持主线程调用。* @param font 字体类型*/static setFont(font: string) {AppUtil.getContext().getApplicationContext().setFont(font);}/*** 获取当前窗口的属性* @param windowClass 窗口实例,默认主窗口* @returns 窗口属性*/static getWindowProperties(windowClass: window.Window = AppUtil.getMainWindow()): window.WindowProperties {return windowClass.getWindowProperties();}/*** 获取虚拟键盘抬起时的页面避让模式* @returns 避让模式*/static getKeyboardAvoidMode(): KeyboardAvoidMode {const mode = AppUtil.getUIContext().getKeyboardAvoidMode();if(typeof mode === 'string'){return mode === "KeyBoardAvoidMode.RESIZE" ? KeyboardAvoidMode.RESIZE : KeyboardAvoidMode.OFFSET;}return mode;}/*** 设置虚拟键盘弹出时,页面的避让模式。* @param value 避让模式* @returns 是否设置成功*/static setKeyboardAvoidMode(value: KeyboardAvoidMode): boolean {try {AppUtil.getUIContext().setKeyboardAvoidMode(value);return true;} catch (err) {console.error('设置键盘避让模式失败:', err);return false;}}/*** 设置窗口的显示方向属性* @param orientation 显示方向* @param windowClass 窗口实例,默认主窗口*/static async setPreferredOrientation(orientation: window.Orientation, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setPreferredOrientation(orientation);}/*** 设置屏幕亮度值* @param brightness 屏幕亮度值,取值范围为[0.0, 1.0]或-1.0* @param windowClass 窗口实例,默认主窗口*/static async setWindowBrightness(brightness: number, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setWindowBrightness(brightness);}/*** 设置屏幕是否为常亮状态* @param isKeepScreenOn 是否常亮* @param windowClass 窗口实例,默认主窗口*/static async setWindowKeepScreenOn(isKeepScreenOn: boolean, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setWindowKeepScreenOn(isKeepScreenOn);}/*** 设置窗口是否为隐私模式* @param isPrivacyMode 是否隐私模式* @param windowClass 窗口实例,默认主窗口*/static async setWindowPrivacyMode(isPrivacyMode: boolean, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setWindowPrivacyMode(isPrivacyMode);}/*** 设置窗口的背景色* @param color 背景色* @param windowClass 窗口实例,默认主窗口*/static setWindowBackgroundColor(color: string, windowClass: window.Window = AppUtil.getMainWindow()) {try {windowClass.setWindowBackgroundColor(color);} catch (err) {console.error('设置窗口背景色失败:', err);}}/*** 设置点击时是否支持切换焦点窗口* @param isFocusable 是否支持切换焦点窗口* @param windowClass 窗口实例,默认主窗口*/static async setWindowFocusable(isFocusable: boolean, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setWindowFocusable(isFocusable);}/*** 设置窗口是否为可触状态* @param isTouchable 是否可触* @param windowClass 窗口实例,默认主窗口*/static async setWindowTouchable(isTouchable: boolean, windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {return windowClass.setWindowTouchable(isTouchable);}/*** 获取状态栏的高度,单位为px。* @returns 状态栏高度*/static getStatusBarHeight(): number {try {const avoidArea = AppUtil.getMainWindow().getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);return avoidArea.topRect.height;} catch (err) {console.error('获取状态栏高度失败:', err);return 0;}}/*** 获取底部导航条的高度,单位为px。* @returns 导航条高度*/static getNavigationIndicatorHeight(): number {try {const avoidArea = AppUtil.getMainWindow().getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);return avoidArea.bottomRect.height;} catch (err) {console.error('获取导航条高度失败:', err);return 0;}}/*** 设置沉浸式状态栏* @param isLayoutFullScreen 是否沉浸式布局* @param enable 是否显示状态栏和导航栏* @param color 窗口背景颜色* @param systemBarProperties 状态栏和导航栏属性*/static async setStatusBar(isLayoutFullScreen: boolean = true, enable: boolean = true, color: string = '#FFFFFF', systemBarProperties?: window.SystemBarProperties) {try {const windowClass = AppUtil.getMainWindow();await windowClass.setWindowLayoutFullScreen(isLayoutFullScreen);windowClass.setWindowBackgroundColor(color);await windowClass.setWindowSystemBarEnable(enable ? ['status', 'navigation'] : []);await windowClass.setSpecificSystemBarEnabled("navigationIndicator", enable);if (systemBarProperties) {await windowClass.setWindowSystemBarProperties(systemBarProperties);}} catch (err) {console.error('设置沉浸式状态栏失败:', err);}}/*** 获取当前应用的BundleInfo* @param sync 是否同步获取,默认异步* @returns BundleInfo*/static async getBundleInfo(sync: boolean = false): Promise<bundleManager.BundleInfo> {return sync ? bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION) : bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);}/*** 获取应用包的名称。* @returns 应用包名*/static async getBundleName(): Promise<string> {const bundleInfo = await AppUtil.getBundleInfo();return bundleInfo.name;}/*** 获取应用版本号。* @returns 版本号*/static async getVersionCode(): Promise<number> {const bundleInfo = await AppUtil.getBundleInfo();return bundleInfo.versionCode;}/*** 获取应用版本名。* @returns 版本名*/static async getVersionName(): Promise<string> {const bundleInfo = await AppUtil.getBundleInfo();return bundleInfo.versionName;}/*** 获取运行应用包所需要最高SDK版本号。* @returns 目标版本号*/static async getTargetVersion(): Promise<number> {const bundleInfo = await AppUtil.getBundleInfo();return bundleInfo.targetVersion;}/*** 获取应用程序的配置信息* @returns 应用信息*/static async getAppInfo(): Promise<bundleManager.ApplicationInfo> {const bundleInfo = await AppUtil.getBundleInfo();return bundleInfo.appInfo;}/*** 主动退出整个应用*/static exit() {AppUtil.getContext().terminateSelf();AppUtil.getContext().getApplicationContext().killAllProcesses();}
}

相关文章:

鸿蒙NEXT开发App相关工具类

import bundleManager from ohos.bundle.bundleManager; import { KeyboardAvoidMode, window } from kit.ArkUI; import { common, ConfigurationConstant } from kit.AbilityKit;/*** App相关工具类(使用该工具前请在UIAbility的onWindowStageCreate方法中调用AppUtil的init方…...

Kafka 的高可用性

Kafka 的高可用性主要通过副本机制、ISR&#xff08;In-Sync Replicas&#xff09;列表和控制器 Broker 来实现。这些机制共同确保了 Kafka 集群在部分节点故障时仍然可以正常运行&#xff0c;数据不会丢失&#xff0c;并且服务不会中断。 1. 副本机制 Kafka 的副本机制是其高…...

docker 部署 postgresql 切换用户

① 启动容器 docker run -d --name postgres-e POSTGRES_PASSWORDpostgres-p 5432:5432 postgres su - omm gsql -d postgres -p 5432 # 将会在postgres下创建用户test1&#xff0c;在其他数据库下是无法删除此用户 CREATE USER test1 WITH Sysadmin IDENTIFIED BY Zcxzhf175…...

Allegro界面颜色改变设置

概述&#xff1a;本文主要讲解如何改变allegro的背景颜色&#xff0c;改为自己喜欢的颜色 1、 打开Allegro文件 2、 Setup—User Preference—UI—General—Allegro_theme选择Light即可 改变前 改变后...

【log4j】配置Slf4j

配置Slf4j 引入lombok包 <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.36</version><scope>provided</scope> </dependency>引入log4j相关api <dependency…...

ThreadLocal与Cookie + Session?

这篇文章主要在做 Echo 社区项目的时候写的&#xff0c;在保持用户登录态的这个需求下&#xff0c;为啥要用 ThreadLocal 存储用户信息&#xff0c;而不是采用常见的 Cookie Session。 Cookie Session 由于 HTTP 协议是无状态的&#xff0c;完成操作关闭浏览器后&#xff0c;…...

freecad手动装插件 add on

python工作台输入 FreeCAD.ConfigGet("UserAppData") 在返回的地址上新建文件夹&#xff1a;Mod #like /home/chen/snap/freecad/common 进入Mod #like /home/chen/snap/freecad/common/Mod git clone 你要的项目 #like git clone https://github.com/looooo/f…...

【算法】二分查找(下)

一、山峰数组的峰顶索引 题目链接&#xff1a;852. 山脉数组的峰顶索引 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给定一个长度为 n 的整数 山脉 数组 arr &#xff0c;其中的值递增到一个 峰值元素 然后递减。 返回峰值元素的下标。 你必须设计并实现时…...

【动手学深度学习】#6 卷积神经网络

主要参考学习资料&#xff1a; 《动手学深度学习》阿斯顿张 等 著 【动手学深度学习 PyTorch版】哔哩哔哩跟李牧学AI 由于本系列一开始跳过了第一章引言部分&#xff0c;因此系列编号比书本章节编号提前。现改为和书本统一&#xff08;因为之前自己的原始笔记也是按照书本章节编…...

认识一家公司:瑞芯微(Rockchip Electronics Co., Ltd.)以及旗下的两款芯片RK3288\RK3588

瑞芯微&#xff08;Rockchip Electronics Co., Ltd.&#xff09;简介 一、公司概况 瑞芯微电子股份有限公司&#xff08;简称“瑞芯微”&#xff09;成立于2001年&#xff0c;总部位于中国福建省福州市&#xff0c;是一家专注于集成电路设计与研发的高新技术企业。公司采用Fa…...

爬虫面试题

总结一下最近面试遇到的笔试题 1、解释Python中的init方法的作用。 在Python中&#xff0c;__init__方法是一种特殊的构造方法&#xff0c;主要用于在创建类的实例时初始化对象。至少接受至少一个参数&#xff1a;self,它是对当前实例的引用&#xff0c;可以通过添加其他参数…...

Netty——零拷贝

文章目录 1. 什么是零拷贝&#xff1f;2. 为什么需要零拷贝&#xff1f;2.1 传统 I/O 的拷贝流程2.2 零拷贝的优化2.2.1 通过 sendfile 系统调用2.2.2 通过 mmap (内存映射) 系统调用 3. Netty 实现零拷贝的方式3.1 文件传输优化&#xff1a;FileRegion 封装3.2 直接内存 (Dire…...

Java制作简单的聊天室(复习)

设计的知识点&#xff1a;几乎包含java基础的全部知识点&#xff08;java基础语法&#xff0c;java基础进阶&#xff1a;双列集合&#xff0c;io流&#xff0c;多线程&#xff0c;网络编程等&#xff09; 代码如下 客户端&#xff1a; 服务器采用的时多线程的循环多线程的方式…...

ES 字段的映射定义了字段的类型及其行为

在 Elasticsearch 中&#xff0c;字段的映射定义了字段的类型及其行为。你提供的 content_answer 字段映射如下&#xff1a; Json 深色版本 "content_answer": { "type": "text", "fields": { "keyword": { …...

Android开发点击字符串web链接跳到系统浏览器上

Android开发点击字符串web链接跳到系统浏览器上 直接上代码&#xff1a;用到你就拿去用 public static void performItemUrlClick(View view, String contentUrl) {if (!TextUtils.isEmpty(contentUrl)) {Intent intent new Intent();if (!contentUrl.startsWith("http…...

运维规则之总结(Summary of Operation and Maintenance Rules)

运维规则之总结 在运维领域&#xff0c;经验和流程往往决定了系统的稳定性与可靠性。一个运维人&#xff0c;总结出了以下10条运维规则&#xff0c;涵盖了从基础管理到高级策略的全面内容&#xff0c;旨在帮助运维人员更好地应对各种挑战&#xff0c;确保系统的平稳运行。 1.…...

智能家居赋能宠物经济:未来宠物行业的另一片蓝海

一、引言&#xff1a;宠物经济的范式转移 随着城市化进程的加速&#xff0c;宠物在现代家庭中的地位日益重要&#xff0c;宠物经济蓬勃发展。近年来&#xff0c;智能家居技术的兴起为宠物行业带来了新的变革&#xff0c;从传统的情感消费模式向技术赋能的精细化养宠模式转变。…...

C++Primer学习(13.6 对象移动)

13.6 对象移动 新标准的一个最主要的特性是可以移动而非拷贝对象的能力。如我们在13.1.1节(第440页)中所见&#xff0c;很多情况下都会发生对象拷贝。在其中某些情况下&#xff0c;对象拷贝后就立即被销毁了。在这些情况下&#xff0c;移动而非拷贝对象会大幅度提升性能。 如我…...

RHCE工程师特训指南

RHCE&#xff08;红帽认证工程师&#xff09;是Linux领域极具含金量的认证之一&#xff0c;其考试以实操为主&#xff0c;注重系统管理、网络服务配置及自动化运维能力。以下内容可帮助对RHCE考生高效规划学习路径。 一、RHCE认证概述 认证结构 RHCE认证分为两部分&#xff…...

内核、进程和线程---操作系统

操作系统 操作系统位于用户程序和硬件之间&#xff0c;通过系统调用提供接口可以让应用程序去使用硬件&#xff0c;但是硬件资源的管理和安全控制由操作系统负责。 用户空间和内存空间 在计算机系统中&#xff0c;内存可以分为两大区域&#xff1a;内核空间&#xff08;Ker…...

如何在 Postman 中上传图片并在请求中正确引用?

Postman 是一款常用的 API 测试工具&#xff0c;它不仅可以测试 API 的请求和响应&#xff0c;还支持多种数据格式包括图片。如何在 Postman 中传输图片&#xff1f; Postman 如何上传图片并在请求中使用教程...

平板实现 adb connect 连接的步骤

1. 检查设备的开发者选项 确保平板设备已开启开发者模式&#xff0c;并启用了USB调试。 2. 检查设备和电脑的网络连接 确保平板和电脑连接到同一个Wi-Fi网络&#xff0c;确认设备的 IP 地址是否正确。 通过 ping 命令测试&#xff1a; ping 192.168.3.243. 通过USB线进行初…...

安全+低碳+高效:Acrel-3000助力企业打造未来型电能管理体系-安科瑞黄安南

一 背景 电能因为方便传输、易于转换、便于控制等特性&#xff0c;成为广大企事业单位生产、办公最主要的能量来源。双碳背景下&#xff0c;由于电能清洁、高效、零排放的特点&#xff0c;能源消费侧将逐步以电代煤、以电代油、以电代气&#xff0c;形成以电为中心的能源消费体…...

专注自习室:番茄工作法实践

专注自习室&#xff1a;番茄工作法实践 我需要一个任务管理工具&#xff0c;但在网上找了很多都找不到合适的工具。市面上的大多数产品过于强调任务完成性&#xff0c;给我带来了很强的心理压力&#xff0c;这种压力最终反而降低了我的工作效率。于是我决定自己动手&#xff0…...

docker save如何迁移镜像更节省空间?

文章目录 方法一&#xff1a;使用docker save命令方法二&#xff1a;直接保存多个镜像到一个tar文件哪个方法更节省磁盘空间&#xff1f;空间效率对比实际测试示例其他优势结论 如何用脚本迁移加载镜像 迁移镜像时候&#xff0c;往往会碰到基础镜像相同的很多镜像需要迁移&…...

LeetCode算法题(Go语言实现)_16

题目 给定一个二进制数组 nums 和一个整数 k&#xff0c;假设最多可以翻转 k 个 0 &#xff0c;则返回执行操作后 数组中连续 1 的最大个数 。 一、代码实现 func longestOnes(nums []int, k int) int {left, zeroCnt, maxLen : 0, 0, 0for right : 0; right < len(nums); …...

CORDIC算法:三角函数的硬件加速革命——从数学原理到FPGA实现的超高效计算方案

计算机该如何求解三角函数&#xff1f;或许你的第一印象是采用泰勒展开&#xff0c;或者采用多项式进行逼近。对于前者&#xff0c;来回的迭代计算开销成本很大&#xff1b;对于后者&#xff0c;多项式式逼近在较窄的范围內比较接近&#xff0c;超过一定范围后&#xff0c;就变…...

JVM 面经

1、什么是 JVM? JVM 就是 Java 虚拟机&#xff0c;它是 Java 实现跨平台的基石。程序运行之前&#xff0c;需要先通过编译器将 Java 源代码文件编译成 Java 字节码文件&#xff1b;程序运行时&#xff0c;JVM 会对字节码文件进行逐行解释&#xff0c;翻译成机器码指令&#x…...

SEO(搜索引擎优化)详解

SEO&#xff08;搜索引擎优化&#xff09;详解 SEO是Search Engine Optimization的缩写&#xff0c;中文称为"搜索引擎优化"。它是指通过一系列技术和方法&#xff0c;提高网站在搜索引擎自然&#xff08;非付费&#xff09;搜索结果中的排名&#xff0c;从而获得更…...

Ubuntu平台下安装Node相关环境

说明&#xff1a;在进行VUE、TS等开发需要用到NodeJS相关环境&#xff0c;不同的项目有时候需要不同的Node版本支撑。本文将详细讲解NVM、Node、Yarn、PM2等环境安装的实施步骤。 测试服务器环境&#xff1a;22.04 LTS。 1. NVM 定义&#xff1a;Node Version Manager&#x…...