目录

天气数据开放<Weather>

功能概述

开放天气App中天气相关数据:配置Weather标签后,在Weather标签中声明Var变量并使用指定的变量名,即可声明气象、空气质量指数、温度、湿度、风向、日出日落时间、月出月落时间、穿衣指数、运动指数、感冒指数、洗车指数等天气数据对应的变量。变量支持默认值,如果获取到实际天气情况会更新该值。

功能说明

  1. 目前该功能暂时只支持国内,后续支持海外会另行通知。
  2. 天气数据来源于华为天气app,使用前请确保华为天气app已使用,并有当前定位城市的天气信息。
  3. 当前天气信息的更新频率为应用时立即更新一次,应用后1小时左右更新一次。
  4. 将主题app版本更新至12.0.12.300之后,下载天气类主题无需授权位置信息。

XML规范

<Weather>
	<Var name="Weather.today.weatherIcon" expression="0" type="int"/>
	<Var name="Weather.today.aqivaluetext" expression="'轻度污染'" type="string"/>
	<Var name="Weather.yesterday.weatherIcon" expression="4" type="int"/>
	<Var name="Weather.yesterday.aqivaluetext" expression="'良好'" type="string"/>
	<Var name="Weather.tomorrow.weatherIcon" expression="13" type="int"/>
	<Var name="Weather.tomorrow.aqivaluetext" expression="'优质'" type="string"/>
</Weather>

只有在Weather标签中声明Var变量并使用指定的变量名,才能获取到实际的天气数据。Var用法参照自定义变量<Var>,天气变量名称参照天气变量名称说明表。

天气变量名称说明表

在此只列举今天的数据,昨天和明天的数据只需要将today改成yesterday和tomorrow即可。

部分数据不支持昨天和明天,文档列表中已有详细说明。

变 量

类  型

注 释

Weather.today.weatherIcon

数值

气象码。

0/9/10/27/28:未知; 1/33:晴; 2/34:大部晴天; 3:局部晴天; 4:局部多云; 5/37:霾; 6:大部多云; 7:多云; 8/75:阴; 11:雾; 12:阵雨; 13:大部多云,伴有阵雨; 14:局部晴天,伴有阵雨; 15:雷阵雨; 16:大部多云,伴有雷阵雨; 17:局部晴天,伴有雷阵雨; 18:雨; 19:阵雪; 20:大部多云,伴有阵雪; 21:局部晴天,伴有阵雪; 22:雪; 23:大部多云,伴有雪; 24:冰; 25/29:雨夹雪; 26:冻雨; 30:炎热; 31:寒冷; 32:有风; 35:局部多云,有阵雨; 36:局部多云; 38:大部多云; 39:局部多云,伴有阵雨; 40:大部多云,伴有阵雨; 41:局部多云,伴有雷阵雨; 42:大部多云,伴有雷阵雨; 43:大部多云,伴有阵雪; 44:大部多云,伴有雪; 45:雷阵雨,伴有冰雹; 46:小雨; 47:中雨; 48:大雨; 49:暴雨; 50:大暴雨; 51:特大暴雨; 52:小雪; 53:中雪; 54:大雪; 55:暴雪; 56:沙尘暴; 57:小到中雨; 58:中到大雨; 59:大到暴雨; 60:暴雨到大暴雨; 61:大暴雨到特大暴雨; 62:小雪到中雪; 63:中雪到大雪; 64:大到暴雪; 65:浮尘; 66:扬沙; 67:强沙尘暴; 68:浓雾; 69:强浓雾; 70:中度霾; 71:重度霾; 72:严重霾; 73:大雾; 74:特强浓雾; 76:吹雪。

说明:

实际使用过程中,可对气象码进行分类展示,以提高制作效率,参照示例3:气象码分类和展示。

Weather.today.weatherIconDes

字符串

气象描述。

0/9/10/27/28:未知; 1/33:晴; 2/34:大部晴天; 3:局部晴天; 4:局部多云; 5/37:霾; 6:大部多云; 7:多云; 8/75:阴; 11:雾; 12:阵雨; 13:大部多云,伴有阵雨; 14:局部晴天,伴有阵雨; 15:雷阵雨; 16:大部多云,伴有雷阵雨; 17:局部晴天,伴有雷阵雨; 18:雨; 19:阵雪; 20:大部多云,伴有阵雪; 21:局部晴天,伴有阵雪; 22:雪; 23:大部多云,伴有雪; 24:冰; 25/29:雨夹雪; 26:冻雨; 30:炎热; 31:寒冷; 32:有风; 35:局部多云,有阵雨; 36:局部多云; 38:大部多云; 39:局部多云,伴有阵雨; 40:大部多云,伴有阵雨; 41:局部多云,伴有雷阵雨; 42:大部多云,伴有雷阵雨; 43:大部多云,伴有阵雪; 44:大部多云,伴有雪; 45:雷阵雨,伴有冰雹; 46:小雨; 47:中雨; 48:大雨; 49:暴雨; 50:大暴雨; 51:特大暴雨; 52:小雪; 53:中雪; 54:大雪; 55:暴雪; 56:沙尘暴; 57:小到中雨; 58:中到大雨; 59:大到暴雨; 60:暴雨到大暴雨; 61:大暴雨到特大暴雨; 62:小雪到中雪; 63:中雪到大雪; 64:大到暴雪; 65:浮尘; 66:扬沙; 67:强沙尘暴; 68:浓雾; 69:强浓雾; 70:中度霾; 71:重度霾; 72:严重霾; 73:大雾; 74:特强浓雾; 76:吹雪。

Weather.today.aqivalue

数值

空气质量指数值:1. 优质;2. 良好;3. 轻度污染;4. 中度污染;5. 重度污染;6. 严重污染;0. 未知。

说明:

无昨天和明天数据。

Weather.today.aqivaluetext

字符串

空气质量等级划分枚举值:优质;良好;轻度污染; 中度污染;重度污染;严重污染。

说明:

无昨天和明天数据。

Weather.today.currentTem

数值

当前温度。

说明:

无昨天和明天数据。

Weather.today.maxtemp

数值

最高温度。

Weather.today.mintemp

数值

最低温度。

Weather.today.humidity

字符串

湿度。

说明:

无昨天和明天数据。

Weather.today.winddir

字符串

风向枚举值:NE,E,SE, S,SW,W,N,NW。

Weather.today.winddirDes

字符串

风向枚举值:NE 东北风,E 东风,SE 东南风, S 南风,SW 西南风,W 西风,N 北风,NW 西北风。

Weather.today.sunRise

字符串

日出 HH:mm格式 06:05。

说明:

无昨天数据。

Weather.today.sunSet

字符串

日落 HH:mm格式 18:05。

说明:

无昨天数据。

Weather.today.moonRise

字符串

月出 HH:mm格式 18:05。

说明:

EMUI11.0版本以下不支持此变量。无昨天数据。

Weather.today.moonSet

字符串

月落 HH:mm格式 06:05。

说明:

EMUI11.0版本以下不支持此变量。无昨天数据。

Weather.today.dressingLevel

数值

穿衣指数:1 羽绒服-寒冷;2 厚外套-冷;3 毛衣-较冷;4 薄外套-较舒适;5 长袖-舒适;6 短袖-热;7 短袖-炎热。

说明:

无昨天和明天数据。

Weather.today.sportsLevel

数值

运动指数:1 适宜运动;2 较适宜运动;3 较不宜运动。

说明:

无昨天和明天数据。

Weather.today.coldLevel

数值

感冒指数:1 感冒少发;2 感冒较易发;3 感冒易发;4 感冒极易发。

说明:

无昨天和明天数据。

Weather.today.carWashLevel

数值

洗车指数:1 不宜;2 较不宜;3 较适宜;4 适宜。

说明:

无昨天和明天数据。

使用说明

  1. 可以搭配天气数据刷新命令RefreshWeatherCommand来刷新天气数据。参照示例2:刷新天气数据。
  2. 在锁屏和桌面万象小组件中使用天气数据变量时,需要通过Intent命令的方式跳转至天气App,以便用户在想了解更多天气信息的时候,可以直接跳转至系统天气App,提升用户体验。
     <!-- 示例:跳转至天气应用,跳转方式为click -->
        <Button y="0" x="0" w="940" h="940">
           <Trigger action="click">
              <IntentCommand action="android.intent.action.MAIN" package="com.huawei.android.totemweather" condition="1" class="com.huawei.android.totemweather.WeatherHome"/>
           </Trigger>
        </Button>
  3. 如果使用了Weather标签并定义了天气变量,可以通过全局变量weatherRespCode的值,判断天气数据是网络返回值还是默认值:全局变量weatherRespCode的值为0表示默认值,1表示网络返回值。参照示例1:展示天气状况并判断数据状态。
    <Text x="30" y="100" alignV="top" color="#ffffff" size="30" format="数据状态0默认1接口返回值: %s" width ="1000" autoLineFeed="true" paras="#weatherRespCode"/>
  4. 天气变量全局持久化:当桌面用到天气数据变量时,使用globalPersist参数并赋值为"true"即开启天气变量全局持久化,实现桌面天气数据与锁屏保持一致。

    参数

    类型

    选项

    注释

    globalPersist

    boolean

    选填

    变量全局持久化;true或者false,默认为false。设置为true即开启变量全局持久化,会优先读取本地同名变量是否有值,该变量会在锁屏、桌面、折叠屏不同状态(折叠、打开、翻转)中同步,如果没有则赋予expression或者默认值。注意不可以和persist同时使用。

    当桌面用到天气数据变量时,使用globalPersist参数并赋值为"true"即开启天气变量全局持久化,实现桌面天气数据与锁屏保持一致。

应用示例

[h2]示例1:展示天气状况并判断数据状态

展示昨天、今天、明天的天气状况,并通过变量weatherRespCode的值判断:天气数据是网络返回值还是默认值。

<Weather>
	<Var name="Weather.today.weatherIcon" expression="0" type="int"/>
	<Var name="Weather.today.aqivaluetext" expression="'优质'" type="string"/>
	<Var name="Weather.yesterday.weatherIcon" expression="4" type="int"/>
	<Var name="Weather.yesterday.aqivaluetext" expression="'良好'" type="string"/>
	<Var name="Weather.tomorrow.weatherIcon" expression="13" type="int"/>
	<Var name="Weather.tomorrow.aqivaluetext" expression="'轻度污染'" type="string"/>
</Weather>

<Text x="30" y="100" alignV="top" color="#ffffff" size="30" format="数据状态0默认1接口返回值: %s" width ="1000" autoLineFeed="true" paras="#weatherRespCode"/>
<Text x="40" y="200"  alignV="top" color="#ffffff" size="30" format="今天天气状况码: %s" width ="1000"  autoLineFeed="true" paras="#Weather.today.weatherIcon"/>
<Text x="40" y="300"  alignV="top" color="#ffffff" size="30" format="今天空气质量: %s" width ="1000" autoLineFeed="true" paras="#Weather.today.aqivaluetext"/>
<Text x="40" y="400"  alignV="top" color="#ffffff" size="30" format="昨天天气状况码: %s" width ="1000"  autoLineFeed="true" paras="#Weather.yesterday.weatherIcon"/>
<Text x="40" y="500"  alignV="top" color="#ffffff" size="30" format="昨天空气质量: %s" width ="1000" autoLineFeed="true" paras="#Weather.yesterday.aqivaluetext"/>
<Text x="40" y="600"  alignV="top" color="#ffffff" size="30" format="明天天气状况码: %s" width ="1000"  autoLineFeed="true" paras="#Weather.tomorrow.weatherIcon"/>
<Text x="40" y="700"  alignV="top" color="#ffffff" size="30" format="明天空气质量: %s" width ="1000" autoLineFeed="true" paras="#Weather.tomorrow.aqivaluetext"/>

[h2]示例2:刷新天气数据

通过点击事件刷新当天的天气数据:

<Weather>
	<Var name="Weather.today.weatherIcon" expression="999" type="int"/>
	<Var name="Weather.today.aqivaluetext" expression="'bbb'" type="string"/>
	<Var name="Weather.today.currentTem" expression="222" type="int"/>
	<Var name="Weather.today.maxtemp" expression="333" type="int"/>
	<Var name="Weather.today.mintemp" expression="444" type="int"/>
</Weather>

<Image x="600" y="500" h="250" w = "260" src="bj.jpg"  />
<Button x="600" y="500" h="250" w="260">
	<Trigger action="down">
		<RefreshWeatherCommand />
	</Trigger>
</Button>

亮屏刷新当天的天气数据:

<Weather>
	<Var name="Weather.today.weatherIcon" expression="999" type="int"/>
	<Var name="Weather.today.aqivaluetext" expression="'bbb'" type="string"/>
	<Var name="Weather.today.currentTem" expression="222" type="int"/>
	<Var name="Weather.today.maxtemp" expression="333" type="int"/>
	<Var name="Weather.today.mintemp" expression="444" type="int"/>
</Weather>

<ExternalCommands>
        <Trigger action="resume">
               <RefreshWeatherCommand/>
        </Trigger>
</ExternalCommands>

[h2]示例3:气象码分类和展示

获取天气气象码,根据不同的气象码值展示不同的天气图标。并将气象码分为以下11大类进行展示:

  1. 晴 sunny(1/33:晴;2/34:大部晴天;3:局部晴天)。
  2. 多云 cloudy(4:局部多云;6:大部多云;7:多云;8/75:阴;36:局部多云;38:大部多云)。
  3. 霾 haze(5/37:霾;70:中度霾;71:重度霾;72:严重霾)。
  4. 雾 fog(11:雾;68:浓雾;69:强浓雾;73:大雾;74:特强浓雾)。
  5. 雨 rain(12:阵雨;13:大部多云,伴有阵雨;14:局部晴天,伴有阵雨;18:雨;26:冻雨;35:局部多云,有阵雨;39:局部多云,伴有阵雨;40:大部多云,伴有阵雨;46:小雨;47:中雨;48:大雨;49:暴雨;50:大暴雨;51:特大暴雨;57:小到中雨;58:中到大雨;59:大到暴雨;60:暴雨到大暴雨;61:大暴雨到特大暴雨)。
  6. 雷阵雨 Storm(15:雷阵雨;16:大部多云,伴有雷阵雨;17:局部晴天,伴有雷阵雨;41:局部多云,伴有雷阵雨;42:大部多云,伴有雷阵雨;45:雷阵雨,伴有冰雹)。
  7. 雪 Snow(19:阵雪;20:大部多云,伴有阵雪;21:局部晴天,伴有阵雪;22:雪;23:大部多云,伴有雪;24:冰;25/29:雨夹雪;43:大部多云,伴有阵雪;44:大部多云,伴有雪;52:小雪;53:中雪;54:大雪;55:暴雪;62:小雪到中雪;63:中雪到大雪;64:大到暴雪;76:吹雪)。
  8. 沙尘暴 Thunder shower(56:沙尘暴;65:浮尘;66:扬沙;67:强沙尘暴)。
  9. 寒冷 cold(31:寒冷)。
  10. 炎热 hot(30:炎热)。
  11. 默认值 default(32:有风;0/9/10/27/28:未知)。
<!--气象码分类-->
<Weather> 
     <Var name="Weather.today.weatherIcon" expression="0" type="int"/>
</Weather>
<Var name="cnweatherIcon" expression="ifelse(eq(#Weather.today.weatherIcon,1),1,eq(#Weather.today.weatherIcon,33),1,eq(#Weather.today.weatherIcon,2),1,eq(#Weather.today.weatherIcon,34),1,eq(#Weather.today.weatherIcon,3),1,eq(#Weather.today.weatherIcon,4),2,eq(#Weather.today.weatherIcon,6),2,eq(#Weather.today.weatherIcon,7),2,eq(#Weather.today.weatherIcon,8),2,eq(#Weather.today.weatherIcon,75),2,eq(#Weather.today.weatherIcon,36),2,eq(#Weather.today.weatherIcon,38),2,eq(#Weather.today.weatherIcon,5),3,eq(#Weather.today.weatherIcon,37),3,eq(#Weather.today.weatherIcon,70),3,eq(#Weather.today.weatherIcon,71),3,eq(#Weather.today.weatherIcon,72),3,eq(#Weather.today.weatherIcon,11),4,eq(#Weather.today.weatherIcon,68),4,eq(#Weather.today.weatherIcon,69),4,eq(#Weather.today.weatherIcon,73),4,eq(#Weather.today.weatherIcon,74),4,eq(#Weather.today.weatherIcon,12),5,eq(#Weather.today.weatherIcon,13),5,eq(#Weather.today.weatherIcon,14),5,eq(#Weather.today.weatherIcon,18),5,eq(#Weather.today.weatherIcon,26),5,eq(#Weather.today.weatherIcon,35),5,eq(#Weather.today.weatherIcon,39),5,eq(#Weather.today.weatherIcon,40),5,eq(#Weather.today.weatherIcon,46),5,eq(#Weather.today.weatherIcon,47),5,eq(#Weather.today.weatherIcon,48),5,eq(#Weather.today.weatherIcon,49),5,eq(#Weather.today.weatherIcon,50),5,eq(#Weather.today.weatherIcon,51),5,eq(#Weather.today.weatherIcon,57),5,eq(#Weather.today.weatherIcon,58),5,eq(#Weather.today.weatherIcon,59),5,eq(#Weather.today.weatherIcon,60),5,eq(#Weather.today.weatherIcon,61),5,eq(#Weather.today.weatherIcon,15),6,eq(#Weather.today.weatherIcon,16),6,eq(#Weather.today.weatherIcon,17),6,eq(#Weather.today.weatherIcon,41),6,eq(#Weather.today.weatherIcon,42),6,eq(#Weather.today.weatherIcon,45),6,eq(#Weather.today.weatherIcon,19),7,eq(#Weather.today.weatherIcon,20),7,eq(#Weather.today.weatherIcon,21),7,eq(#Weather.today.weatherIcon,22),7,eq(#Weather.today.weatherIcon,23),7,eq(#Weather.today.weatherIcon,24),7,eq(#Weather.today.weatherIcon,25),7,eq(#Weather.today.weatherIcon,29),7,eq(#Weather.today.weatherIcon,43),7,eq(#Weather.today.weatherIcon,44),7,eq(#Weather.today.weatherIcon,52),7,eq(#Weather.today.weatherIcon,53),7,eq(#Weather.today.weatherIcon,54),7,eq(#Weather.today.weatherIcon,55),7,eq(#Weather.today.weatherIcon,62),7,eq(#Weather.today.weatherIcon,63),7,eq(#Weather.today.weatherIcon,64),7,eq(#Weather.today.weatherIcon,76),7,eq(#Weather.today.weatherIcon,56),8,eq(#Weather.today.weatherIcon,65),8,eq(#Weather.today.weatherIcon,66),8,eq(#Weather.today.weatherIcon,67),8,eq(#Weather.today.weatherIcon,31),9,eq(#Weather.today.weatherIcon,30),10,11)"/>

<!--天气图标展示-->
<Group>
     <Image x="450" y="490" w="80" h="90" src="weather_sunny.png" visibility="eq(#cnweatherIcon,1)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_cloudy.png" visibility="eq(#cnweatherIcon,2)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_haze.png" visibility="eq(#cnweatherIcon,3)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_fog.png" visibility="eq(#cnweatherIcon,4)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_rain.png" visibility="eq(#cnweatherIcon,5)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_storm.png" visibility="eq(#cnweatherIcon,6)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_snow.png" visibility="eq(#cnweatherIcon,7)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_thunder_shower.png" visibility="eq(#cnweatherIcon,8)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_cold.png" visibility="eq(#cnweatherIcon,9)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_hot.png" visibility="eq(#cnweatherIcon,10)" align="center" alignV="center"/>
     <Image x="450" y="490" w="80" h="90" src="weather_default.png" visibility="eq(#cnweatherIcon,11)" align="center" alignV="center"/>
</Group>