【概述】app实现天气查询是再正常不过的功能了,又因为往往不止一个activity去获取数据,那就想到封装一个类,需要时去调用获取即可。
【高德api地址】https://lbs.amap.com/api/android-sdk/guide/map-data/weather
【注】因为看文档还有点懵,故将自己抓脑写的代码记录下,以便查询
【思路】activity发送请求--获取地址--根据地址获取天气--回调给activity
1、获取当前城市或区域
因为文档中是直接提供地点进行查询,所以首先获取到地点,用到了GPSUtils类(参见前个blog:https://blog.csdn.net/gzyh_tech/article/details/82491214)。
mquery = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_LIVE);
2、跟着高德文档的感觉走,即可顺利获取到天气数据。
3.重点还是放在activity的通信上。
page1定义一个静态属性方法接口:
// 查询天气回调
public interface Event {
public void setWeather(String weather,String city,String tem);
}
public static class EventManager {
private static Event mEvent;
public static void setEventListener(Event nm) {
mEvent = nm;
}
public static void raiseEvent(String weather,String city,String tem) {
mEvent.setWeather(weather,city,tem);
}
}
// 查询天气回调
private Event event = new Event() {
@Override
public void setWeather(String weather,String city,String tem) {
id_address.setText(city);
Toast.makeText(getActivity(),city+",天气:"+weather+",温度:"+tem,Toast.LENGTH_SHORT).show();
}
};
page2得到数据后调用返回即可:
weatherFragment.EventManager.raiseEvent(weather,address,temperature);
主要代码如下:
package menu.bottombar.bottombar.Utils;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearchQuery;
import menu.bottombar.bottombar.Fragment.homeFragment;
import menu.bottombar.bottombar.Utils.GPSUtils.GPSUtils;
public class WeatherSearch extends com.amap.api.services.weather.WeatherSearch implements com.amap.api.services.weather.WeatherSearch.OnWeatherSearchListener,AMapLocationListener {
private String city;
private String address;
private String weather;
private String temperature;
private Context context;
private LayoutInflater layoutInflater;
private TextView mTextView;
private int code;
/*高德地图定位*/
private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
private String[] strMsg;
public WeatherSearch(Context context) {
super(context);
this.layoutInflater= LayoutInflater.from(context);
}
public void getWeather(){
WeatherSearchQuery mquery = new WeatherSearchQuery(city+"", WeatherSearchQuery.WEATHER_TYPE_LIVE);
com.amap.api.services.weather.WeatherSearch mweathersearch=new com.amap.api.services.weather.WeatherSearch(context);
mweathersearch.setOnWeatherSearchListener(this);
mweathersearch.setQuery(mquery);
mweathersearch.searchWeatherAsyn(); //异步搜索
}
public void getLocalToWeather(Context context,int sCode){
code=sCode;
this.context=context;
Location();
}
@Override
public void onWeatherLiveSearched(LocalWeatherLiveResult weatherLiveResult, int rCode) {
if (rCode == 1000) {
if (weatherLiveResult != null&&weatherLiveResult.getLiveResult() != null) {
LocalWeatherLive weatherlive = weatherLiveResult.getLiveResult();
weather=weatherlive.getWeather();
temperature=weatherlive.getTemperature();
Log.i("Weather",weatherlive.getTemperature()+"");
Log.i("Weather",weatherlive.getWeather()+"");
/*reporttime1.setText(weatherlive.getReportTime()+"发布");
weather.setText(weatherlive.getWeather());
Temperature.setText(weatherlive.getTemperature()+"°");
wind.setText(weatherlive.getWindDirection()+"风 "+weatherlive.getWindPower()+"级");
humidity.setText("湿度 "+weatherlive.getHumidity()+"%");*/
switch (code){
case 1:
weatherFragment.EventManager.raiseEvent(weather,address,temperature);
break;
default:
break ;
}
}else {
// Log.i("Weather","获取天气失败!");
}
}else {
// Log.i("Weather","获取天气失败!api失败");
}
}
@Override
public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
}
Handler mHandler = new Handler() {
public void dispatchMessage(android.os.Message msg) {
switch (msg.what) {
//定位完成
case GPSUtils.MSG_LOCATION_FINISH:
String result = "";
try {
AMapLocation loc = (AMapLocation) msg.obj;
result = GPSUtils.getLocationStr(loc, 1);
strMsg = result.split(",");
address=strMsg[4];
city=strMsg[3];
//Log.e("Weather","city:"+city+",address:"+address+"!");
//Toast.makeText(context, "定位成功", Toast.LENGTH_SHORT).show();
getWeather();
} catch (Exception e) {
//Log.e("Weather", "定位失败"+e.toString());
}
break;
default:
//Log.e("Weather","定位失败:"+msg+"");
break;
}
};
};
public void Location() {
// TODO Auto-generated method stub
try {
locationClient = new AMapLocationClient(context);
locationOption = new AMapLocationClientOption();
// 设置定位模式为低功耗模式
locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
// 设置定位监听
locationClient.setLocationListener((AMapLocationListener) this);
locationOption.setOnceLocation(true);//设置为单次定位
locationClient.setLocationOption(locationOption);// 设置定位参数
// 启动定位
locationClient.startLocation();
mHandler.sendEmptyMessage(GPSUtils.MSG_LOCATION_START);
} catch (Exception e) {
}
}
@Override
public void onLocationChanged(AMapLocation loc) {
if (null != loc) {
Message msg = mHandler.obtainMessage();
msg.obj = loc;
msg.what = GPSUtils.MSG_LOCATION_FINISH;
mHandler.sendMessage(msg);
}
}
}