目录

Assigning Permissions in the Manifest File

Before calling the weather awareness capability, assign required permissions in the manifest file.

  1. <!-- Location permission. This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Importing API Classes

To use the weather awareness capability, you need to import the public capability classes of HUAWEI Awareness Kit, and also the weather-related classes.

  1. import com.huawei.hmf.tasks.OnFailureListener;
  2. import com.huawei.hmf.tasks.OnSuccessListener;
  3. import com.huawei.hms.kit.awareness.Awareness;
  4. // Import the weather capture-related classes.
  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;

Developing Capabilities

1. Obtain the Capture Client object of HUAWEI Awareness Kit.

2. Call the weather query capability API through the Capture Client object to obtain the user's context.

3. Enable your app to listen for the result returned by HUAWEI Awareness Kit for further processing.

  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.                //For more weather information, see the API Reference of Awareness Kit.
  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.        });