import java.util.List;
import android.content.Context;
import android.widget.Toast;
import com.amap.api.location.AMapLocalDayWeatherForecast;
import com.amap.api.location.AMapLocalWeatherForecast;
import com.amap.api.location.AMapLocalWeatherListener;
import com.amap.api.location.AMapLocalWeatherLive;
import com.amap.api.location.LocationManagerProxy;
public class CurrentWeatherReport implements AMapLocalWeatherListener {
private LocationManagerProxy mLocationManagerProxy;
Context mContext;
public CurrentWeatherReport(Context context) {
this.mContext = context;
}
/**
* 注册天气监听
*
* @param i
* LocationManagerProxy.WEATHER_TYPE_LIVE(实况天气)
* LocationManagerProxy.WEATHER_TYPE_FORECAST(天气预报)
*/
public void init(int i) {
// WEATHER_TYPE_LIVE(实况天气)WEATHER_TYPE_FORECAST(天气预报)
// listener为天气监听
mLocationManagerProxy = LocationManagerProxy.getInstance(mContext);
mLocationManagerProxy.requestWeatherUpdates(i, this);
}
// 注意:如果需要同时获取实时天气、未来天气数据时,请确保获取当前位置后再调用。分开调用时,请忽略本句。
@Override
public void onWeatherLiveSearched(AMapLocalWeatherLive aMapLocalWeatherLive) {
// AMapLocalWeatherLive类为实况天气类,通过该对象可获取当前的气温、风向、风力、空气湿度百分比等天气情况。
if (aMapLocalWeatherLive != null
&& aMapLocalWeatherLive.getAMapException().getErrorCode() == 0) {
String city = aMapLocalWeatherLive.getCity();// 城市
String weather = aMapLocalWeatherLive.getWeather();// 天气情况
String windDir = aMapLocalWeatherLive.getWindDir();// 风向
String windPower = aMapLocalWeatherLive.getWindPower();// 风力
String humidity = aMapLocalWeatherLive.getHumidity();// 空气湿度
String reportTime = aMapLocalWeatherLive.getReportTime();// 数据发布时间
Toast.makeText(
mContext,
city + "\n" + weather + "\n" + windDir + "\n" + windPower
+ "\n" + humidity + "\n" + reportTime,
Toast.LENGTH_SHORT).show();
} else {
// 获取天气预报失败
Toast.makeText(mContext, "获取天气预报失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onWeatherForecaseSearched(
AMapLocalWeatherForecast aMapLocalWeatherForecast) {
// AMapLocalWeatherForecast 为天气预报类,可获取当前时间在内的3天的天气情况。
if (aMapLocalWeatherForecast != null
&& aMapLocalWeatherForecast.getAMapException().getErrorCode() == 0) {
List<AMapLocalDayWeatherForecast> forcasts = aMapLocalWeatherForecast
.getWeatherForecast();
for (int i = 0; i < forcasts.size(); i++) {
AMapLocalDayWeatherForecast forcast = forcasts.get(i);
switch (i) {
// 今天天气
case 0:
// 城市
String city = forcast.getCity();
String today = "今天 ( " + forcast.getDate() + " )";
String todayWeather = forcast.getDayWeather() + " "
+ forcast.getDayTemp() + "/"
+ forcast.getNightTemp() + " "
+ forcast.getDayWindPower();
Toast.makeText(mContext,
city + "\n" + today + "\n" + todayWeather,
Toast.LENGTH_SHORT).show();
break;
// 明天天气
case 1:
String tomorrow = "明天 ( " + forcast.getDate() + " )";
String tomorrowWeather = forcast.getDayWeather() + " "
+ forcast.getDayTemp() + "/"
+ forcast.getNightTemp() + " "
+ forcast.getDayWindPower();
Toast.makeText(mContext, tomorrow + "\n" + tomorrowWeather,
Toast.LENGTH_SHORT).show();
break;
// 后天天气
case 2:
String aftertomorrow = "后天( " + forcast.getDate() + " )";
String aftertomorrowWeather = forcast.getDayWeather()
+ " " + forcast.getDayTemp() + "/"
+ forcast.getNightTemp() + " "
+ forcast.getDayWindPower();
Toast.makeText(mContext,
aftertomorrow + "\n" + aftertomorrowWeather,
Toast.LENGTH_SHORT).show();
break;
}
}
} else {
// 获取天气预报失败
Toast.makeText(mContext, "获取天气预报失败", Toast.LENGTH_SHORT).show();
}
}
}
转载于:https://my.oschina.net/spoon2014/blog/420984