【概要】アプリ天気は、通常のクエリ機能で実現し、また、取得データに、多くの場合、複数のアクティビティので、その後、必要なときに取得するために呼び出すために、クラスのパッケージを考えます。
[高]デAPIアドレスhttps://lbs.amap.com/api/android-sdk/guide/map-data/weather
ドキュメントを参照するには、[注]少し無知なので、それはクエリに書き込みコードに彼の次のレコードの脳をキャッチします
[思考]の活動が要求を送信する - 活動へのコールバック - 天気予報を取得するためのアドレス - アドレスを取得
図1に示すように、現在の都市や地域を取得します
ドキュメントを直接位置照会を設けているため、その最初のサイト、用途GPSUtilsクラス(最後のブログを参照してくださいに取得https://blog.csdn.net/gzyh_tech/article/details/82491214を)。
mquery = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_LIVE);
2は、行くには高い道徳的な文書の気持ちと一緒に、あなたは、気象データへのアクセスをスムーズにすることができます。
3.焦点は、通信の活動です。
方法の1ページ目のインターフェースは、静的プロパティを定義します。
// 查询天气回调
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);
}
}
}