已合并
Weather Service Kit #34
weitingting创建于 9月24日
Weather Service Kit #34
已合并
从已删除 :master合入到HarmonyOS_Samples/application-services-snippetsmaster
共 30 个文件变更+974-0
| @@ -0,0 +1,10 @@ | |||
| 1 | +{ | ||
| 2 | + "app": { | ||
| 3 | + "bundleName": "xxx.xxx.xxx.xxx", | ||
| 4 | + "vendor": "example", | ||
| 5 | + "versionCode": 1000000, | ||
| 6 | + "versionName": "1.0.0", | ||
| 7 | + "icon": "$media:app_icon", | ||
| 8 | + "label": "$string:app_name" | ||
| 9 | + } | ||
| 10 | +} | ||
| @@ -0,0 +1,8 @@ | |||
| 1 | +{ | ||
| 2 | + "string": [ | ||
| 3 | + { | ||
| 4 | + "name": "app_name", | ||
| 5 | + "value": "MyApplication" | ||
| 6 | + } | ||
| 7 | + ] | ||
| 8 | +} | ||
| @@ -0,0 +1,78 @@ | |||
| 1 | + Copyright (c) 2026 Huawei Device Co., Ltd. All rights reserved. | ||
| 2 | + | ||
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + you may not use this file except in compliance with the License. | ||
| 5 | + You may obtain a copy of the License at | ||
| 6 | + | ||
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + | ||
| 9 | + Unless required by applicable law or agreed to in writing, software | ||
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + See the License for the specific language governing permissions and | ||
| 13 | + limitations under the License. | ||
| 14 | + | ||
| 15 | +Apache License, Version 2.0 | ||
| 16 | +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
| 17 | + | ||
| 18 | +1. Definitions. | ||
| 19 | + | ||
| 20 | +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. | ||
| 21 | + | ||
| 22 | +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. | ||
| 23 | + | ||
| 24 | +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. | ||
| 25 | + | ||
| 26 | +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. | ||
| 27 | + | ||
| 28 | +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. | ||
| 29 | + | ||
| 30 | +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. | ||
| 31 | + | ||
| 32 | +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). | ||
| 33 | + | ||
| 34 | +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. | ||
| 35 | + | ||
| 36 | +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." | ||
| 37 | + | ||
| 38 | +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. | ||
| 39 | + | ||
| 40 | +2. Grant of Copyright License. | ||
| 41 | + | ||
| 42 | +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. | ||
| 43 | + | ||
| 44 | +3. Grant of Patent License. | ||
| 45 | + | ||
| 46 | +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. | ||
| 47 | + | ||
| 48 | +4. Redistribution. | ||
| 49 | + | ||
| 50 | +You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: | ||
| 51 | +1.You must give any other recipients of the Work or Derivative Works a copy of this License; and | ||
| 52 | +2.You must cause any modified files to carry prominent notices stating that You changed the files; and | ||
| 53 | +3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and | ||
| 54 | +4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. | ||
| 55 | + | ||
| 56 | +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. | ||
| 57 | + | ||
| 58 | +5. Submission of Contributions. | ||
| 59 | + | ||
| 60 | +Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. | ||
| 61 | + | ||
| 62 | +6. Trademarks. | ||
| 63 | + | ||
| 64 | +This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. | ||
| 65 | + | ||
| 66 | +7. Disclaimer of Warranty. | ||
| 67 | + | ||
| 68 | +Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. | ||
| 69 | + | ||
| 70 | +8. Limitation of Liability. | ||
| 71 | + | ||
| 72 | +In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. | ||
| 73 | + | ||
| 74 | +9. Accepting Warranty or Additional Liability. | ||
| 75 | + | ||
| 76 | +While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. | ||
| 77 | + | ||
| 78 | +END OF TERMS AND CONDITIONS | ||
| @@ -0,0 +1,42 @@ | |||
| 1 | +## 天气服务 | ||
| 2 | + | ||
| 3 | +### 介绍 | ||
| 4 | +Weather Service Kit(天气服务)是鸿蒙生态下的一个数据提供服务, | ||
| 5 | +Weather Service Kit融合了多家气象行业TOPs供应商,提供专业、精准、稳定的超本地化天气数据服务, | ||
| 6 | +开发者可以通过Weather Service Kit提供的开放能力获取天气数据。 | ||
| 7 | + | ||
| 8 | +### 工程目录 | ||
| 9 | +``` | ||
| 10 | +├──entry/src/main/ets/ | ||
| 11 | +│ ├──entryability | ||
| 12 | +│ │ └──EntryAbility.ts // 本地启动Ability,加载WeatherPage | ||
| 13 | +│ ├──pages | ||
| 14 | +│ │ └──WeatherPage.ets // 天气主页面,发起定位和天气请求 | ||
| 15 | +│ ├──component | ||
| 16 | +│ │ ├──CustomCurrentSheet.ets // 当前天气展示组件 | ||
| 17 | +│ │ └──CustomDailySheet.ets // 多日天气预报展示组件 | ||
| 18 | +│ └──utils | ||
| 19 | +│ └──LocationService.ets // 定位服务,处理权限申请和获取位置 | ||
| 20 | +└──entry/src/main/resources // 应用静态资源目录 | ||
| 21 | +``` | ||
| 22 | + | ||
| 23 | +### 具体实现 | ||
| 24 | +1. **权限申请**:使用 `LocationService.requestLocationPermission()` 申请定位权限 | ||
| 25 | +2. **获取位置**:通过 `LocationService.getCurrentLocation()` 获取当前经纬度 | ||
| 26 | +3. **构建请求**:创建 `WeatherRequest` 对象,设置经纬度和数据集类型 | ||
| 27 | +4. **获取天气**:调用 `weatherService.getWeather(request)` 获取天气数据 | ||
| 28 | +5. **展示结果**:分别使用 `CustomCurrentSheet` 展示实况天气、`CustomDailySheet` 展示多日预报 | ||
| 29 | + | ||
| 30 | +### 相关权限 | ||
| 31 | +需要在[AppGallery Connect](https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/)网站上开通天气服务权限 ,并重新申请Profile文件进行签名 | ||
| 32 | + | ||
| 33 | +### 约束与限制 | ||
| 34 | +1、硬件要求设备类型:Phone、Tablet、PC/2in1、Wearable。 | ||
| 35 | + | ||
| 36 | +2、HarmonyOS系统:HarmonyOS NEXT Developer Beta2及以上。 | ||
| 37 | + | ||
| 38 | +3、软件要求DevEco Studio版本:DevEco Studio NEXT Developer Beta2及以上。 | ||
| 39 | + | ||
| 40 | +4、HarmonyOS SDK版本:HarmonyOS NEXT Developer Beta2 SDK及以上。 | ||
| 41 | + | ||
| 42 | +5、Weather Service Kit当前仅面向系统应用开放,暂不对外开放。 | ||
| @@ -0,0 +1,56 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | + | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved. | ||
| 4 | +# | ||
| 5 | + | ||
| 6 | +set -e # 遇到错误时退出 | ||
| 7 | + | ||
| 8 | +echo "开始时间:【$(date '+%H:%M:%S')】" | ||
| 9 | + | ||
| 10 | +# 获取项目根目录 | ||
| 11 | +project_path=$(cd "$(dirname "$0")" && pwd) | ||
| 12 | +echo "项目路径: ${project_path}" | ||
| 13 | + | ||
| 14 | +cd "${project_path}" || exit 1 | ||
| 15 | + | ||
| 16 | +# ========================================================== | ||
| 17 | +# 安装依赖(可选) | ||
| 18 | +# ========================================================== | ||
| 19 | +if [ -f "oh-package.json5" ] && command -v ohpm &> /dev/null; then | ||
| 20 | + echo "安装依赖包..." | ||
| 21 | + ohpm install --all || echo "警告: 依赖安装失败,继续构建..." | ||
| 22 | +fi | ||
| 23 | + | ||
| 24 | +# ========================================================== | ||
| 25 | +# 开始构建 | ||
| 26 | +# ========================================================== | ||
| 27 | +echo "" | ||
| 28 | +echo "********** 开始构建HAP包 **********" | ||
| 29 | +echo "构建类型: release" | ||
| 30 | +echo "产品: default" | ||
| 31 | + | ||
| 32 | +# 使用默认配置构建 | ||
| 33 | +hvigorw clean assembleHap --no-daemon | ||
| 34 | + | ||
| 35 | +echo "" | ||
| 36 | +echo "********** 构建完成 **********" | ||
| 37 | +echo "结束时间:【$(date '+%H:%M:%S')】" | ||
| 38 | + | ||
| 39 | +# ========================================================== | ||
| 40 | +# 查找HAP文件 | ||
| 41 | +# ========================================================== | ||
| 42 | +echo "" | ||
| 43 | +echo "查找生成的HAP文件..." | ||
| 44 | + | ||
| 45 | +hap_file=$(find "${project_path}" -name "*.hap" -type f 2>/dev/null | head -1) | ||
| 46 | + | ||
| 47 | +if [ -n "${hap_file}" ]; then | ||
| 48 | + echo "成功生成HAP文件: ${hap_file}" | ||
| 49 | + echo "" | ||
| 50 | + echo "HAP文件信息:" | ||
| 51 | + ls -lh "${hap_file}" | ||
| 52 | +else | ||
| 53 | + echo "警告: 未找到HAP文件,请检查构建日志" | ||
| 54 | +fi | ||
| 55 | + | ||
| 56 | +exit 0 | ||
| @@ -0,0 +1,28 @@ | |||
| 1 | +{ | ||
| 2 | + "apiType": "stageMode", | ||
| 3 | + "buildOption": { | ||
| 4 | + }, | ||
| 5 | + "buildOptionSet": [ | ||
| 6 | + { | ||
| 7 | + "name": "release", | ||
| 8 | + "arkOptions": { | ||
| 9 | + "obfuscation": { | ||
| 10 | + "ruleOptions": { | ||
| 11 | + "enable": true, | ||
| 12 | + "files": [ | ||
| 13 | + "./obfuscation-rules.txt" | ||
| 14 | + ] | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + }, | ||
| 19 | + ], | ||
| 20 | + "targets": [ | ||
| 21 | + { | ||
| 22 | + "name": "default" | ||
| 23 | + }, | ||
| 24 | + { | ||
| 25 | + "name": "ohosTest", | ||
| 26 | + } | ||
| 27 | + ] | ||
| 28 | +} | ||
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; | ||
| 2 | + | ||
| 3 | +export default { | ||
| 4 | + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ | ||
| 5 | + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ | ||
| 6 | +} | ||
| @@ -0,0 +1,18 @@ | |||
| 1 | +# Define project specific obfuscation rules here. | ||
| 2 | +# You can include the obfuscation configuration files in the current module's build-profile.json5. | ||
| 3 | +# | ||
| 4 | +# For more details, see | ||
| 5 | +# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md | ||
| 6 | + | ||
| 7 | +# Obfuscation options: | ||
| 8 | +# -disable-obfuscation: disable all obfuscations | ||
| 9 | +# -enable-property-obfuscation: obfuscate the property names | ||
| 10 | +# -enable-toplevel-obfuscation: obfuscate the names in the global scope | ||
| 11 | +# -compact: remove unnecessary blank spaces and all line feeds | ||
| 12 | +# -remove-log: remove all console.* statements | ||
| 13 | +# -print-namecache: print the name cache that contains the mapping from the old names to new names | ||
| 14 | +# -apply-namecache: reuse the given cache file | ||
| 15 | + | ||
| 16 | +# Keep options: | ||
| 17 | +# -keep-property-name: specifies property names that you want to keep | ||
| 18 | +# -keep-global-name: specifies names that you want to keep in the global scope | ||
| @@ -0,0 +1,10 @@ | |||
| 1 | +{ | ||
| 2 | + "name": "entry", | ||
| 3 | + "version": "1.0.0", | ||
| 4 | + "description": "Please describe the basic information.", | ||
| 5 | + "main": "", | ||
| 6 | + "author": "", | ||
| 7 | + "license": "", | ||
| 8 | + "dependencies": {} | ||
| 9 | +} | ||
| 10 | + | ||
| @@ -0,0 +1,150 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +// [Start get_weather_import] | ||
| 17 | +import { weatherService } from '@kit.WeatherServiceKit'; | ||
| 18 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 19 | +import { hilog } from '@kit.PerformanceAnalysisKit'; | ||
| 20 | +// [End get_weather_import] | ||
| 21 | +import { CustomCurrentSheet } from '../component/CustomCurrentSheet'; | ||
| 22 | +import { CustomDailySheet } from '../component/CustomDailySheet'; | ||
| 23 | +import { LocationService } from '../utils/LocationService'; | ||
| 24 | + | ||
| 25 | +const DOMAIN = 0x0000; | ||
| 26 | +const TAG = 'WeatherPageTAG'; | ||
| 27 | + | ||
| 28 | +@Entry | ||
| 29 | +@Component | ||
| 30 | +struct WeatherPage { | ||
| 31 | + @State weather: weatherService.Weather = {} as weatherService.Weather; | ||
| 32 | + @State latitude: number = 22.62; | ||
| 33 | + @State longitude: number = 114.07; | ||
| 34 | + @State errorMessage: string = ''; | ||
| 35 | + | ||
| 36 | + build() { | ||
| 37 | + Scroll() { | ||
| 38 | + Column() { | ||
| 39 | + this.TitleComponent() | ||
| 40 | + this.ButtonComponent() | ||
| 41 | + this.ResultDisplayComponent() | ||
| 42 | + } | ||
| 43 | + .padding(10) | ||
| 44 | + .width('100%') | ||
| 45 | + .height('100%') | ||
| 46 | + .backgroundColor($r('app.color.light_gray')) | ||
| 47 | + } | ||
| 48 | + .height('100%') | ||
| 49 | + .scrollBar(BarState.Off) | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + @Builder | ||
| 53 | + TitleComponent() { | ||
| 54 | + Text('Weather Service Kit') | ||
| 55 | + .fontSize(20) | ||
| 56 | + .fontWeight(FontWeight.Bold) | ||
| 57 | + .margin({ bottom: 10 }) | ||
| 58 | + .alignSelf(ItemAlign.Center) | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Builder | ||
| 62 | + ButtonComponent() { | ||
| 63 | + Button('获取天气数据', { type: ButtonType.Normal, stateEffect: true }) | ||
| 64 | + .borderRadius(8) | ||
| 65 | + .backgroundColor('#317aff') | ||
| 66 | + .width(140) | ||
| 67 | + .height(40) | ||
| 68 | + .margin({ bottom: 10 }) | ||
| 69 | + .alignSelf(ItemAlign.Center) | ||
| 70 | + .onClick(() => { | ||
| 71 | + this.getLocationWeather(this.getUIContext().getHostContext()); | ||
| 72 | + }) | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Builder | ||
| 76 | + ResultDisplayComponent() { | ||
| 77 | + Text('返回数据一览') | ||
| 78 | + .fontSize(16) | ||
| 79 | + .fontWeight(FontWeight.Bold) | ||
| 80 | + .margin({ bottom: 10 }) | ||
| 81 | + .alignSelf(ItemAlign.Center) | ||
| 82 | + | ||
| 83 | + if (this.errorMessage) { | ||
| 84 | + Text(this.errorMessage) | ||
| 85 | + .fontSize(14) | ||
| 86 | + .fontColor('#ff4444') | ||
| 87 | + .margin({ bottom: 10 }) | ||
| 88 | + .alignSelf(ItemAlign.Center) | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + if (this.weather.current) { | ||
| 92 | + CustomCurrentSheet({ current: this.weather.current }) | ||
| 93 | + .margin({ bottom: 20 }) | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + if (this.weather.daily) { | ||
| 97 | + CustomDailySheet({ daily: this.weather.daily }) | ||
| 98 | + .margin({ bottom: 20 }) | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + async getLocationWeather(context: Context | undefined): Promise<void> { | ||
| 103 | + if (!context) { | ||
| 104 | + this.errorMessage = '上下文异常'; | ||
| 105 | + return; | ||
| 106 | + } | ||
| 107 | + const permissionResult = await LocationService.requestLocationPermission(context); | ||
| 108 | + if (permissionResult !== 0) { | ||
| 109 | + this.errorMessage = '定位权限申请失败'; | ||
| 110 | + return; | ||
| 111 | + } | ||
| 112 | + const location = await LocationService.getCurrentLocation(); | ||
| 113 | + if (location.isError) { | ||
| 114 | + this.errorMessage = '定位请求失败'; | ||
| 115 | + return; | ||
| 116 | + } | ||
| 117 | + this.errorMessage = ''; | ||
| 118 | + this.latitude = location.latitude; | ||
| 119 | + this.longitude = location.longitude; | ||
| 120 | + this.getWeather(); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + private async getWeather(): Promise<void> { | ||
| 124 | + // [Start get_weather_request_body] | ||
| 125 | + let request: weatherService.WeatherRequest = { | ||
| 126 | + location: { | ||
| 127 | + latitude: this.latitude, | ||
| 128 | + longitude: this.longitude | ||
| 129 | + }, | ||
| 130 | + limitedDatasets: [weatherService.Dataset.CURRENT, weatherService.Dataset.ALERTS] | ||
| 131 | + }; | ||
| 132 | + // [End get_weather_request_body] | ||
| 133 | + // [Start get_weather] | ||
| 134 | + try { | ||
| 135 | + let weather: weatherService.Weather = await weatherService.getWeather(request); | ||
| 136 | + if (weather.current) { | ||
| 137 | + hilog.info(DOMAIN, TAG, `getWeather current temperature: ${weather.current.temperature}`); | ||
| 138 | + } | ||
| 139 | + if (weather.alerts?.length) { | ||
| 140 | + hilog.info(DOMAIN, TAG, `getWeather alert: ${weather.alerts[0].title}`); | ||
| 141 | + } | ||
| 142 | + this.weather = weather; | ||
| 143 | + } catch (err) { | ||
| 144 | + let error = err as BusinessError; | ||
| 145 | + this.errorMessage = `获取天气失败: ${error.message}`; | ||
| 146 | + hilog.error(DOMAIN, TAG, `getWeather failed. Code: ${error.code}, message: ${error.message}`); | ||
| 147 | + } | ||
| 148 | + // [End get_weather] | ||
| 149 | + } | ||
| 150 | +} | ||
| @@ -0,0 +1,88 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +// [Start get_current_location_import] | ||
| 17 | +import { geoLocationManager } from '@kit.LocationKit'; | ||
| 18 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 19 | +import { hilog } from '@kit.PerformanceAnalysisKit'; | ||
| 20 | +// [End get_current_location_import] | ||
| 21 | +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; | ||
| 22 | + | ||
| 23 | +const DOMAIN = 0x0000; | ||
| 24 | +const TAG = 'LocationService'; | ||
| 25 | + | ||
| 26 | +export interface LocationData { | ||
| 27 | + latitude: number; | ||
| 28 | + longitude: number; | ||
| 29 | + isError: boolean; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +export class LocationService { | ||
| 33 | + private static readonly LOCATION_PERMISSIONS: Permissions[] = [ | ||
| 34 | + 'ohos.permission.LOCATION', | ||
| 35 | + 'ohos.permission.APPROXIMATELY_LOCATION' | ||
| 36 | + ]; | ||
| 37 | + | ||
| 38 | + static async requestLocationPermission(context: Context): Promise<number> { | ||
| 39 | + try { | ||
| 40 | + let atManager = abilityAccessCtrl.createAtManager(); | ||
| 41 | + return atManager.requestPermissionsFromUser(context, LocationService.LOCATION_PERMISSIONS).then((data) => { | ||
| 42 | + let ret: number[] = data.authResults; | ||
| 43 | + let dialogShownResults: boolean[] = data.dialogShownResults || [false]; | ||
| 44 | + let isDialogShown = dialogShownResults[0]; | ||
| 45 | + if (ret[0] === 0) { | ||
| 46 | + hilog.info(DOMAIN, TAG, `permission granted: ${JSON.stringify(ret)}`); | ||
| 47 | + return Promise.resolve(0); | ||
| 48 | + } else if (ret[0] === 2) { | ||
| 49 | + hilog.warn(DOMAIN, TAG, `permission operation error: ${JSON.stringify(ret)}`); | ||
| 50 | + return Promise.resolve(isDialogShown ? 1 : 2); | ||
| 51 | + } else { | ||
| 52 | + hilog.warn(DOMAIN, TAG, `permission rejected: ${JSON.stringify(ret)}`); | ||
| 53 | + return Promise.resolve(isDialogShown ? 1 : 2); | ||
| 54 | + } | ||
| 55 | + }).catch((err: Error) => { | ||
| 56 | + return Promise.resolve(2); | ||
| 57 | + }) | ||
| 58 | + } catch (err) { | ||
| 59 | + return Promise.resolve(2); | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + static async getCurrentLocation(): Promise<LocationData> { | ||
| 64 | + // [Start get_current_location] | ||
| 65 | + return geoLocationManager.getCurrentLocation().then((result) => { | ||
| 66 | + hilog.info(DOMAIN, TAG, `current location latitude: ${result.latitude}, longitude: ${result.longitude}`); | ||
| 67 | + // [StartExclude get_current_location] | ||
| 68 | + let locationData: LocationData = { | ||
| 69 | + latitude: result.latitude, | ||
| 70 | + longitude: result.longitude, | ||
| 71 | + isError: false | ||
| 72 | + }; | ||
| 73 | + return locationData; | ||
| 74 | + // [EndExclude get_current_location] | ||
| 75 | + }).catch((err: BusinessError) => { | ||
| 76 | + hilog.error(DOMAIN, TAG, `getCurrentLocation failed. Code: ${err.code}, message: ${err.message}`); | ||
| 77 | + // [StartExclude get_current_location] | ||
| 78 | + let locationData: LocationData = { | ||
| 79 | + latitude: 0, | ||
| 80 | + longitude: 0, | ||
| 81 | + isError: true | ||
| 82 | + }; | ||
| 83 | + return locationData; | ||
| 84 | + // [EndExclude get_current_location] | ||
| 85 | + }); | ||
| 86 | + // [End get_current_location] | ||
| 87 | + } | ||
| 88 | +} | ||
| @@ -0,0 +1,80 @@ | |||
| 1 | +// [Start internet_permission] | ||
| 2 | +{ | ||
| 3 | + "module": { | ||
| 4 | + // [StartExclude internet_permission] | ||
| 5 | + "name": "entry", | ||
| 6 | + "type": "entry", | ||
| 7 | + "description": "$string:module_desc", | ||
| 8 | + "mainElement": "EntryAbility", | ||
| 9 | + "deviceTypes": [ | ||
| 10 | + "phone", | ||
| 11 | + "tablet", | ||
| 12 | + "tv", | ||
| 13 | + "wearable", | ||
| 14 | + "2in1" | ||
| 15 | + ], | ||
| 16 | + "deliveryWithInstall": true, | ||
| 17 | + "installationFree": false, | ||
| 18 | + "pages": "$profile:main_pages", | ||
| 19 | + "abilities": [ | ||
| 20 | + { | ||
| 21 | + "name": "EntryAbility", | ||
| 22 | + "srcEntry": "./ets/entryability/EntryAbility.ets", | ||
| 23 | + "description": "$string:EntryAbility_desc", | ||
| 24 | + "icon": "$media:layered_image", | ||
| 25 | + "label": "$string:EntryAbility_label", | ||
| 26 | + "startWindowIcon": "$media:startIcon", | ||
| 27 | + "startWindowBackground": "$color:start_window_background", | ||
| 28 | + "exported": true, | ||
| 29 | + "skills": [ | ||
| 30 | + { | ||
| 31 | + "entities": [ | ||
| 32 | + "entity.system.home" | ||
| 33 | + ], | ||
| 34 | + "actions": [ | ||
| 35 | + "action.system.home" | ||
| 36 | + ] | ||
| 37 | + } | ||
| 38 | + ] | ||
| 39 | + } | ||
| 40 | + ], | ||
| 41 | + // [EndExclude internet_permission] | ||
| 42 | + "requestPermissions": [ | ||
| 43 | + { | ||
| 44 | + "name": "ohos.permission.INTERNET", | ||
| 45 | + // [StartExclude internet_permission] | ||
| 46 | + "reason": "$string:internet_reason", | ||
| 47 | + "usedScene": { | ||
| 48 | + "abilities": [ | ||
| 49 | + "MainAbility" | ||
| 50 | + ], | ||
| 51 | + "when": "inuse" | ||
| 52 | + } | ||
| 53 | + // [EndExclude internet_permission] | ||
| 54 | + }, | ||
| 55 | + // [StartExclude internet_permission] | ||
| 56 | + { | ||
| 57 | + "name": "ohos.permission.LOCATION", | ||
| 58 | + "reason": "$string:location_desc", | ||
| 59 | + "usedScene": { | ||
| 60 | + "abilities": [ | ||
| 61 | + "MainAbility" | ||
| 62 | + ], | ||
| 63 | + "when": "inuse" | ||
| 64 | + } | ||
| 65 | + }, | ||
| 66 | + { | ||
| 67 | + "name": "ohos.permission.APPROXIMATELY_LOCATION", | ||
| 68 | + "reason": "$string:location_desc", | ||
| 69 | + "usedScene": { | ||
| 70 | + "abilities": [ | ||
| 71 | + "MainAbility" | ||
| 72 | + ], | ||
| 73 | + "when": "inuse" | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | + // [EndExclude internet_permission] | ||
| 77 | + ] | ||
| 78 | + } | ||
| 79 | +} | ||
| 80 | +// [End internet_permission] | ||
AWeatherServiceKit/WeatherServiceKitSample/entry/src/main/resources/base/media/layered_image.json+7-0
| @@ -0,0 +1,7 @@ | |||
| 1 | +{ | ||
| 2 | + "layered-image": | ||
| 3 | + { | ||
| 4 | + "background" : "$media:background", | ||
| 5 | + "foreground" : "$media:foreground" | ||
| 6 | + } | ||
| 7 | +} | ||
| @@ -0,0 +1,21 @@ | |||
| 1 | +{ | ||
| 2 | + "modelVersion": "5.0.0", | ||
| 3 | + "dependencies": { | ||
| 4 | + }, | ||
| 5 | + "execution": { | ||
| 6 | + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ | ||
| 7 | + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ | ||
| 8 | + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ | ||
| 9 | + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ | ||
| 10 | + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ | ||
| 11 | + }, | ||
| 12 | + "logging": { | ||
| 13 | + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ | ||
| 14 | + }, | ||
| 15 | + "debugging": { | ||
| 16 | + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ | ||
| 17 | + }, | ||
| 18 | + "nodeOptions": { | ||
| 19 | + // "maxOldSpaceSize": 4096 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process */ | ||
| 20 | + } | ||
| 21 | +} | ||
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { appTasks } from '@ohos/hvigor-ohos-plugin'; | ||
| 2 | + | ||
| 3 | +export default { | ||
| 4 | + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ | ||
| 5 | + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ | ||
| 6 | +} | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | +{ | ||
| 2 | + "modelVersion": "5.0.0", | ||
| 3 | + "name": "myapplication", | ||
| 4 | + "version": "1.0.0", | ||
| 5 | + "description": "Please describe the basic information.", | ||
| 6 | + "main": "", | ||
| 7 | + "author": "", | ||
| 8 | + "license": "", | ||
| 9 | + "dependencies": { | ||
| 10 | + }, | ||
| 11 | + "devDependencies": { | ||
| 12 | + } | ||
| 13 | +} | ||