[Huawei Developers] Freemind R:
More details about this article, you can visit HUAWEI Developer Forum

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703060125.59470978476749345694473137730649:50510722025427:2800:420114088F8E848FC31B036F3DB68B49E3026390B98A9A395933287EEF5D00F2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703060125.59470978476749345694473137730649:50510722025427:2800:420114088F8E848FC31B036F3DB68B49E3026390B98A9A395933287EEF5D00F2.png[/IMG]​

Introduction:

Weather Awareness is being used to obtain current weather details such as humidity, temperature, wind direction, rain probability etc.

For calling Weather Awareness capability we have to assign the given permissions in the manifest file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Here we are discussing about the following APIs

WeatherStatus

WeatherId

Let's go to the details.

1. WeatherStatus

public interface WeatherStatus

WeatherStatus carries complete weather information. This API provides five methods.

public abstract WeatherSituation getWeatherSituation() :- For current weather information.

public abstract Aqi getAqi() :- For obtaining the AQI

public abstract List<LiveInfo> getLiveInfo() - For living index in the next one to two days.

public abstract List<DailyWeather> getDailyWeather() :- For obtaining weather in the next seven days

public abstract List<HourlyWeather> getHourlyWeather() :- For obtaining weather in the next 24 hours

Let's go to the details.

public abstract WeatherSituation getWeatherSituation()

This method is used to obtain weather and city information. The weather information includes a weather ID, humidity, Celsius degree, Fahrenheit degree, a somatosensory temperature (°C), a somatosensory temperature (°F), a wind direction, a wind force level, a wind speed, atmospheric pressure, an ultraviolet light intensity, and update time. The city information includes the name, city code, province name, and time zone.

private void getWeatherSituation() {
Awareness.getCaptureClient(this).getWeatherByDevice()
.addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
@Override
public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
WeatherSituation weatherSituation = weatherStatus.getWeatherSituation();
Situation situation = weatherSituation.getSituation();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

String weatherInfoStr =
"Time Zone : " +(weatherSituation.getCity().getTimeZone()) + "\n\n" +
"Weather id : " + situation.getWeatherId() + "\n\n" +
"Temperature : " + situation.getTemperatureC() + "℃" +
"/" + situation.getTemperatureF() + "℉" + "\n\n" +
"Wind speed : " + situation.getWindSpeed() + "km/h" + "\n\n" +
"Wind direction : " + situation.getWindDir() + "\n\n" +
"Humidity : " + situation.getHumidity() + "%";
Log.i(TAG, weatherInfoStr);
weather_info_capture.setTextColor(getColor(R.color.green));
weather_info_capture.setText(weatherInfoStr);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "get weather failed");
weather_info_capture.setTextColor(getColor(R.color.red));
weather_info_capture.setText("get weather failed: "+e);
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054526.39558997005075253545463241886856:50510722025427:2800:C64DD8666F52F702E6371C6D1603DDEAF662B87ED8F795EDD1EBAAA14E572003.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054526.39558997005075253545463241886856:50510722025427:2800:C64DD8666F52F702E6371C6D1603DDEAF662B87ED8F795EDD1EBAAA14E572003.png[/IMG]​

public abstract Aqi getAqi()

This method is used to obtain the AQI information, including data about carbon monoxide, nitrogen dioxide, ozone, PM10, PM2.5, and sulphur dioxide, and comprehensive weather index. Currently it is available only in China.

private void getAqi(){
Awareness.getCaptureClient(this).getWeatherByDevice()
.addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
@Override
public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
Aqi aqi = weatherStatus.getAqi();
String aqi_info="Ozone: "+aqi.getO3()+"\n"+
"Carbon Monoxide"+aqi.getCo()+"\n"+
"Nitrogen Dioxide"+aqi.getNo2()+"\n"+
"PM10"+aqi.getPm10()+"\n"+
"Suphur Dioxide"+aqi.getSo2()+"\n"+
"PM2.5"+aqi.getPm25()+"\n";
weather_info_capture.setText(aqi_info);

}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "getAqi failed");
weather_info_capture.setTextColor(getColor(R.color.red));
weather_info_capture.setText("get Aqi failed " + e);
}
});
}

public abstract List<LiveInfo> getLiveInfo()

This method is used to obtain the living index of the current day and the next one to two days, including the living index (code values), living level, and time information. Currently it is available only in china.

List<LiveInfo> liveInfo = weatherStatus.getLiveInfo();

public abstract List<DailyWeather> getDailyWeather()

This method is used to obtain weather information of the next seven days, including the moon phase, moonrise and moonset time, sunrise and sunset time, highest and lowest temperatures (°C), highest and lowest temperatures (°F), AQI value, date, weather IDs, wind direction, wind force, and wind speed in the daytime and at night.

private void getDailyWeather(){
Awareness.getCaptureClient(this).getWeatherByDevice()
.addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
@Override
public void onSuccess(WeatherStatusResponse weatherStatusResponse) {

WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
List<DailyWeather> dailyWeather = weatherStatus.getDailyWeather();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String daily_info="";
for(int i=0;i<7;i++) {
daily_info += "Date: " + dateFormat.format(dailyWeather.get(i).getDateTimeStamp()) + "\n" +
"Sun Rise: " + dateFormat.format(dailyWeather.get(i).getSunRise()) + "\n" +
"Sun Set: " + dateFormat.format(dailyWeather.get(i).getSunSet()) + "\n" +
"Moon Set: " + dateFormat.format(dailyWeather.get(i).getMoonSet()) + "\n" +
"Moon Rise: " + dateFormat.format(dailyWeather.get(i).getMoonRise()) + "\n" +
"Moon Phase: " + dailyWeather.get(i).getMoonphase() + "\n" +
"Aqi Value: " + dailyWeather.get(i).getAqiValue() + "\n" +
"Temperature Max: " + dailyWeather.get(i).getMaxTempC() + "°C / " + dailyWeather.get(i).getMaxTempF() + "°F \n" +
"Temperature Min: " + dailyWeather.get(i).getMinTempC() + "°C / " + dailyWeather.get(i).getMinTempF() + "°F \n" +
"Day Weather Id: " + dailyWeather.get(i).getSituationDay().getWeatherId() + "\n" +
"Night Weather Id: " + dailyWeather.get(i).getSituationNight().getWeatherId() + "\n" +
"Day Wind Direction: " + dailyWeather.get(i).getSituationDay().getWindDir() + "\n" +
"Night Wind Direction: " + dailyWeather.get(i).getSituationNight().getWindDir() + "\n" +
"Day Wind Level: " + dailyWeather.get(i).getSituationDay().getWindLevel() + "\n" +
"Night Wind Level: " + dailyWeather.get(i).getSituationNight().getWindLevel() + "\n" +
"Day Wind Speed: " + dailyWeather.get(i).getSituationDay().getWindSpeed() + "\n" +
"Night Wind Speed: " + dailyWeather.get(i).getSituationNight().getWindSpeed() + "\n\n";
}
weather_info_capture.setText(daily_info);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "get Hourly weather failed");
weather_info_capture.setTextColor(getColor(R.color.red));
weather_info_capture.setText("get dailyWeather failed " + e);
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054656.15554737752861130498598953765800:50510722025427:2800:1258DA57B3D800C2A8F973EE3A004067D99FC8214CAF52C0A625934E66A9A71C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054656.15554737752861130498598953765800:50510722025427:2800:1258DA57B3D800C2A8F973EE3A004067D99FC8214CAF52C0A625934E66A9A71C.png[/IMG]​

public abstract List<HourlyWeather> getHourlyWeather()

This method is used to obtains weather information in the next 24 hours. Weather information in the next 24 hours, including a weather ID, time information, Celsius degree, Fahrenheit degree, rainfall probability, and whether it is day/night.

private void getHourlyWeather() {
Awareness.getCaptureClient(this).getWeatherByDevice()
.addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
@Override
public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
List<HourlyWeather> hourlyWeather = weatherStatus.getHourlyWeather();
weather_info_capture.setTextColor(getColor(R.color.green));
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String weather_info_hours="";
for(int i=0;i<=24;i++) {

weather_info_hours+= dateFormat.format(hourlyWeather.get(i).getDateTimeStamp()) +" - "+
(hourlyWeather.get(i).isDayNight() ? "Day" : "Night")+"\n"+
"Temperature: " + hourlyWeather.get(i).getTempC() + "°C / "+hourlyWeather.get(i).getTempF() +"°F \n"+
"Rain Probability: " + (hourlyWeather.get(i).getRainprobability()<50?"Low":(hourlyWeather.get(i).getRainprobability()<75?"Medium":"High"))+"\n"+
"Rain Probability: " + hourlyWeather.get(i).getRainprobability()+"\n"+
"Weather Id: " +hourlyWeather.get(i).getWeatherId()+"\n\n";
}

weather_info_capture.setText(weather_info_hours);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "get Hourly weather failed");
weather_info_capture.setTextColor(getColor(R.color.red));
weather_info_capture.setText("get Hourly weather failed " + e);
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054608.84946137035342950989882982416927:50510722025427:2800:5BDA37AEFB9C59C5D237491344226854E205773B86ED47C3C6CC3956F63AB2C8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200703054608.84946137035342950989882982416927:50510722025427:2800:5BDA37AEFB9C59C5D237491344226854E205773B86ED47C3C6CC3956F63AB2C8.png[/IMG]​

2. WeatherId

public interface WeatherId

Weather ID is a constant API, which describes the weather information corresponding to different values of weatherId.

References:

https://developer.huawei.com/consumer/en/doc/development/HMS-References/awareness-weatherstatus-4