文档管理中心
我的

此版本文档已归档不再维护,推荐您使用最新版本。

指南情景感知服务应用开发客户端开发天气感知能力开发

天气感知能力开发

在Manifest指定权限

在调用天气感知能力时,开发者需要先在Manifest中指定相应的权限。

收起
自动换行
深色代码主题
复制
  1. <!-- 位置权限,此权限为敏感权限,声明后还需在代码中动态申请 -->
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

导入接口类

使用天气状态感知能力,除需要导入情景感知服务的公共能力类外,还需要导入天气状态相关的类。

收起
自动换行
深色代码主题
复制
  1. import com.huawei.hmf.tasks.OnFailureListener;
  2. import com.huawei.hmf.tasks.OnSuccessListener;
  3. import com.huawei.hms.kit.awareness.Awareness;
  4. //导入天气相关快照类
  5. import com.huawei.hms.kit.awareness.capture.WeatherStatusResponse;
  6. import com.huawei.hms.kit.awareness.status.WeatherStatus;
  7. import com.huawei.hms.kit.awareness.status.weather.Situation;
  8. import com.huawei.hms.kit.awareness.status.weather.WeatherSituation;

能力开发

1. 获取Awareness Kit的“Capture Client”。

2. 通过“Capture Client”调用状态查询能力接口查询情景状态。

3. 监听情景感知服务结果返回,进行应用的业务处理。

收起
自动换行
深色代码主题
复制
  1. Awareness.getCaptureClient(this).getWeatherByDevice()
  2.        .addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
  3.            @Override
  4.            public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
  5.                WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
  6.                WeatherSituation weatherSituation = weatherStatus.getWeatherSituation();
  7.                Situation situation = weatherSituation.getSituation();
  8.                // 更多返回的天气数据信息可参考华为开发者文档
  9.                String weatherInfoStr = "City:" + weatherSituation.getCity().getName() + "\n" +
  10.                        "Weather id is " + situation.getWeatherId() + "\n" +
  11.                        "CN Weather id is " + situation.getCnWeatherId() + "\n" +
  12.                        "Temperature is " + situation.getTemperatureC() + "℃" +
  13.                        "," + situation.getTemperatureF() + "℉" + "\n" +
  14.                        "Wind speed is " + situation.getWindSpeed() + "km/h" + "\n" +
  15.                        "Wind direction is " + situation.getWindDir() + "\n" +
  16.                        "Humidity is " + situation.getHumidity() + "%";
  17.                Log.i(TAG, weatherInfoStr);
  18.            }
  19.        })
  20.        .addOnFailureListener(new OnFailureListener() {
  21.            @Override
  22.            public void onFailure(Exception e) {
  23.                Log.e(TAG, "get weather failed");
  24.            }
  25.        });
本页面可能包含由第三方许可的内容,请参考具体描述。