首页

android 调用天气预报demo 源码下载

java

2020-7-9

package cn.hhs.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.hhs.util.DataUtil;
import cn.hhs.util.HttpService;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;

/**
 * @author 家铄
 * @QQ 1466181491
 * 
 */
public class WeatherScreen extends Activity implements OnClickListener {
	BMapManager mBMapMan = null;
	LocationListener mLocationListener = null;
	MKSearch mSearch = null;
	String npCityId = "";
	EditText dialogCity;
	String provinceName, cityName;
	boolean flag = true;
	ProgressDialog progressDialog;
	LinearLayout ll_yes, ll_no;
	TextView tv_city, tv_today, tv_attr1, tv_attr2, tv_attr3, tv_noresult;
	TextView tv_date1, tv_date2, tv_wd1, tv_wd2;
	ImageView ima, ima1, ima2;
	Button btn_return, btn_other;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.weather_screen);
		initView();
		initBaiDuMap();

	}

	@Override
	protected void onPause() {
		mBMapMan.getLocationManager().removeUpdates(mLocationListener);
		mBMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
		mBMapMan.getLocationManager().enableProvider(
				MKLocationManager.MK_GPS_PROVIDER);
		mBMapMan.start();
		super.onResume();
	}

	/**
	 * 
	 * 方法名:initBaiDuMap 功能:初始化百度地图 参数:
	 */
	private void initBaiDuMap() {
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("14A97FC2DDF678193F61C19C0A20EA29C49DEF5C", null);// 14A97FC2DDF678193F61C19C0A20EA29C49DEF5C
		mBMapMan.start();
		initMyLocation();
	}

	/**
	 * 
	 * 方法名:initMyLocation 功能:启动定位 参数:
	 */
	private void initMyLocation() {
		progressDialog = ProgressDialog
				.show(this, null, "城市定位中...", true, true);
		mLocationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				if (location != null && flag) {
					progressDialog.dismiss();
					flag = false;
					GeoPoint myPt = new GeoPoint(
							(int) (location.getLatitude() * 1e6),
							(int) (location.getLongitude() * 1e6));
					initMapSerach();
					// 将当前坐标转化为地址获取当前城市名称
					mSearch.reverseGeocode(myPt);
				} else {
				}
			}
		};
	}

	private void initMapSerach() {
		// 初始化搜索模块,注册事件监听
		mSearch = new MKSearch();
		mSearch.init(mBMapMan, new MKSearchListener() {

			public void onGetPoiResult(MKPoiResult res, int type, int error) {
			}

			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
			}

			public void onGetAddrResult(MKAddrInfo res, int error) {
				if (error != 0 || res == null) {
				} else {
					String city = res.addressComponents.city;
					String pro = res.addressComponents.province;
					if (city != null) {
						provinceName = pro.substring(0, pro.length() - 1);
						cityName = city.substring(0, city.length() - 1);
						progressDialog = ProgressDialog.show(
								WeatherScreen.this, null, "天气查询中...", true,
								true);
						QueryAsyncTask asyncTask = new QueryAsyncTask();
						asyncTask.execute("");
					} else {
						Toast.makeText(WeatherScreen.this, "定位不到当前城市,无法查询天气",
								Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {

			}
		});

	}

	/**
	 * 
	 * 方法名:initView 功能:初始化控件 参数:
	 */
	private void initView() {
		ll_yes = (LinearLayout) this.findViewById(R.id.ws2_ll_yes);
		ll_no = (LinearLayout) this.findViewById(R.id.ws2_ll_no);

		tv_city = (TextView) this.findViewById(R.id.ws2_tv_city);
		ima = (ImageView) this.findViewById(R.id.ws2_iv_image);
		tv_attr1 = (TextView) this.findViewById(R.id.ws2_tv_attr1);
		tv_attr2 = (TextView) this.findViewById(R.id.ws2_tv_attr2);
		tv_attr3 = (TextView) this.findViewById(R.id.ws2_tv_attr3);

		tv_noresult = (TextView) this.findViewById(R.id.ws2_tv_noresult);

		tv_date1 = (TextView) this.findViewById(R.id.ws2_tv_1_date);
		tv_date2 = (TextView) this.findViewById(R.id.ws2_tv_2_date);
		tv_wd1 = (TextView) this.findViewById(R.id.ws2_tv_1_wd);
		tv_wd2 = (TextView) this.findViewById(R.id.ws2_tv_2_wd);

		ima1 = (ImageView) this.findViewById(R.id.ws2_iv_1_image);
		ima2 = (ImageView) this.findViewById(R.id.ws2_iv_2_image);

		btn_return = (Button) this.findViewById(R.id.ws2_btn_return);
		btn_return.setOnClickListener(this);
		btn_other = (Button) this.findViewById(R.id.ws2_btn_submit);
		btn_other.setOnClickListener(this);
	}

	private class QueryAsyncTask extends AsyncTask {
		@Override
		protected void onPostExecute(Object result) {
			progressDialog.dismiss();
			if (result != null) {
				String weatherResult = (String) result;
				if (weatherResult.split(";").length > 1) {
					String a = weatherResult.split(";")[1];
					if (a.split("=").length > 1) {
						String b = a.split("=")[1];
						String c = b.substring(1, b.length() - 1);
						String[] resultArr = c.split("\\}");
						if (resultArr.length > 0) {
							todayParse(resultArr[0]);
							tommrowParse(resultArr[1]);
							thirddayParse(resultArr[2]);
							ll_yes.setVisibility(View.VISIBLE);
							tv_city.setText(cityName);
						}

					} else {
						DataUtil.Alert(WeatherScreen.this, "查无天气信息");
					}
				} else {
					DataUtil.Alert(WeatherScreen.this, "查无天气信息");
				}
			} else {
				DataUtil.Alert(WeatherScreen.this, "查无天气信息");
			}
			super.onPostExecute(result);
		}

		@Override
		protected Object doInBackground(Object... params) {
			return HttpService.getWeather(cityName);
		}
	}

	/**
	 * 
	 * 方法名:todayParse 功能:今天天气 参数:
	 * 
	 * @param weather
	 */
	private void todayParse(String weather) {
		String temp = weather.replace("'", "");
		String[] tempArr = temp.split(",");
		String wd = "";
		String tq = "";
		String fx = "";
		if (tempArr.length > 0) {
			for (int i = 0; i < tempArr.length; i  ) {
				if (tempArr[i].indexOf("t1:") != -1) {
					wd = tempArr[i].substring(3, tempArr[i].length())   "℃";
				} else if (tempArr[i].indexOf("t2:") != -1) {
					wd = wd   "~"
							  tempArr[i].substring(3, tempArr[i].length())
							  "℃";
				} else if (tempArr[i].indexOf("d1:") != -1) {
					fx = tempArr[i].substring(3, tempArr[i].length());
				} else if (tempArr[i].indexOf("s1:") != -1) {
					tq = tempArr[i].substring(4, tempArr[i].length());
				}
			}

			tv_attr1.setText("气温:"   wd);
			tv_attr2.setText("天气情况:"   tq);
			tv_attr3.setText("风向:"   fx);
			ima.setImageResource(imageResoId(tq));

		}
	}

	/**
	 * 
	 * 方法名:tommrowParse 功能:明天天气 参数:
	 * 
	 * @param weather
	 */
	private void tommrowParse(String weather) {
		String temp = weather.replace("'", "");
		String[] tempArr = temp.split(",");
		String wd = "";
		String tq = "";
		String fx = "";
		if (tempArr.length > 0) {
			for (int i = 0; i < tempArr.length; i  ) {
				if (tempArr[i].indexOf("t1:") != -1) {
					wd = tempArr[i].substring(3, tempArr[i].length())   "℃";
				} else if (tempArr[i].indexOf("t2:") != -1) {
					wd = wd   "~"
							  tempArr[i].substring(3, tempArr[i].length())
							  "℃";
				} else if (tempArr[i].indexOf("d1:") != -1) {
					fx = tempArr[i].substring(3, tempArr[i].length());
				} else if (tempArr[i].indexOf("s1:") != -1) {
					tq = tempArr[i].substring(4, tempArr[i].length());
				}
			}
			tv_date1.setText("明天    "   tq);
			tv_wd1.setText(wd);
			ima1.setImageResource(imageResoId(tq));

		}
	}

	/**
	 * 
	 * 方法名:thirddayParse 功能:获取后天天气 参数:
	 * 
	 * @param weather
	 */
	private void thirddayParse(String weather) {
		String temp = weather.replace("'", "");
		String[] tempArr = temp.split(",");
		String wd = "";
		String tq = "";
		String fx = "";
		if (tempArr.length > 0) {
			for (int i = 0; i < tempArr.length; i  ) {
				if (tempArr[i].indexOf("t1:") != -1) {
					wd = tempArr[i].substring(3, tempArr[i].length())   "℃";
				} else if (tempArr[i].indexOf("t2:") != -1) {
					wd = wd   "~"
							  tempArr[i].substring(3, tempArr[i].length())
							  "℃";
				} else if (tempArr[i].indexOf("d1:") != -1) {
					fx = tempArr[i].substring(3, tempArr[i].length());
				} else if (tempArr[i].indexOf("s1:") != -1) {
					tq = tempArr[i].substring(4, tempArr[i].length());
				}
			}

			tv_date2.setText("后天    "   tq);
			tv_wd2.setText(wd);
			ima2.setImageResource(imageResoId(tq));

		}
	}

	/**
	 * 
	 * 方法名:imageResoId 功能:获取图片 参数:
	 * 
	 * @param weather
	 * @return
	 */
	private int imageResoId(String weather) {
		int resoid = R.drawable.s_2;
		// 多云转晴,以下类同 // indexOf:包含字串
		if (weather.indexOf("多云") != -1 || weather.indexOf("晴") != -1) {
			resoid = R.drawable.s_1;
		} else if (weather.indexOf("多云") != -1 && weather.indexOf("阴") != -1) {
			resoid = R.drawable.s_2;
		} else if (weather.indexOf("阴") != -1 && weather.indexOf("雨") != -1) {
			resoid = R.drawable.s_3;
		} else if (weather.indexOf("晴") != -1 && weather.indexOf("雨") != -1) {
			resoid = R.drawable.s_12;
		} else if (weather.indexOf("晴") != -1 && weather.indexOf("雾") != -1) {
			resoid = R.drawable.s_12;
		} else if (weather.indexOf("晴") != -1) {
			resoid = R.drawable.s_13;
		} else if (weather.indexOf("多云") != -1) {
			resoid = R.drawable.s_2;
		} else if (weather.indexOf("阵雨") != -1) {
			resoid = R.drawable.s_3;
		} else if (weather.indexOf("小雨") != -1) {
			resoid = R.drawable.s_3;
		} else if (weather.indexOf("中雨") != -1) {
			resoid = R.drawable.s_4;
		} else if (weather.indexOf("大雨") != -1) {
			resoid = R.drawable.s_5;
		} else if (weather.indexOf("暴雨") != -1) {
			resoid = R.drawable.s_5;
		} else if (weather.indexOf("冰雹") != -1) {
			resoid = R.drawable.s_6;
		} else if (weather.indexOf("雷阵雨") != -1) {
			resoid = R.drawable.s_7;
		} else if (weather.indexOf("小雪") != -1) {
			resoid = R.drawable.s_8;
		} else if (weather.indexOf("中雪") != -1) {
			resoid = R.drawable.s_9;
		} else if (weather.indexOf("大雪") != -1) {
			resoid = R.drawable.s_10;
		} else if (weather.indexOf("暴雪") != -1) {
			resoid = R.drawable.s_10;
		} else if (weather.indexOf("扬沙") != -1) {
			resoid = R.drawable.s_11;
		} else if (weather.indexOf("沙尘") != -1) {
			resoid = R.drawable.s_11;
		} else if (weather.indexOf("雾") != -1) {
			resoid = R.drawable.s_12;
		}
		return resoid;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ws2_btn_return:
			finish();
			break;
		case R.id.ws2_btn_submit:
			showOtherCity();
			break;
		}
	}

	/**
	 * 
	 * 方法名:showOtherCity 功能:输入其他城市名称 参数:
	 */
	private void showOtherCity() {
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.weather_other_city,
				(ViewGroup) findViewById(R.id.ws_dialog));
		dialogCity = (EditText) layout.findViewById(R.id.ws_city_name);
		new AlertDialog.Builder(this)
				.setTitle("请输入城市名称")
				.setView(layout)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						cityName = dialogCity.getText().toString();

						if (cityName != null && cityName.length() > 0) {
							progressDialog = ProgressDialog.show(
									WeatherScreen.this, null, "天气查询中...", true,
									true);
							QueryAsyncTask asyncTask = new QueryAsyncTask();
							asyncTask.execute("");
						}
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).show();
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
Android调用天气demo/WeatherDemo/.classpath , 534
Android调用天气demo/WeatherDemo/.project , 847
Android调用天气demo/WeatherDemo/.settings/org.eclipse.jdt.core.prefs , 177
Android调用天气demo/WeatherDemo/AndroidManifest.xml , 1415
Android调用天气demo/WeatherDemo/bin/AndroidManifest.xml , 1415
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/BuildConfig.class , 339
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R$attr.class , 331
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R$drawable.class , 961
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R$id.class , 995
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R$layout.class , 500
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R$string.class , 418
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/R.class , 510
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen$1.class , 1349
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen$2.class , 3046
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen$3.class , 1809
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen$4.class , 850
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen$QueryAsyncTask.class , 2310
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/activity/WeatherScreen.class , 9527
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/util/DataUtil$1.class , 756
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/util/DataUtil.class , 1231
Android调用天气demo/WeatherDemo/bin/classes/cn/hhs/util/HttpService.class , 4380
Android调用天气demo/WeatherDemo/bin/classes.dex , 143628
Android调用天气demo/WeatherDemo/bin/dexedLibs/annotations-87bd6017633eaca0eb88e79414e1aff7.jar , 943
Android调用天气demo/WeatherDemo/bin/dexedLibs/baidumapapi-6bfec2ef735f6d8c2ddf0f862292e19d.jar , 192584
Android调用天气demo/WeatherDemo/bin/dexedLibs/baidumapapi-ff40d2942c83d62cf495ec5d08dc97d1.jar , 192603
Android调用天气demo/WeatherDemo/bin/jarlist.cache , 120
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/listview_line.png , 73
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/main_bg.png , 1290
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_1.png , 4938
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_10.png , 5413
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_11.png , 5268
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_12.png , 3371
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_13.png , 4403
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_2.png , 3269
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_3.png , 3992
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_4.png , 4619
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_5.png , 4611
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_6.png , 6294
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_7.png , 5590
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_8.png , 4356
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable/s_9.png , 4967
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable-hdpi/icon.png , 3966
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable-ldpi/icon.png , 1537
Android调用天气demo/WeatherDemo/bin/res/crunch/drawable-mdpi/icon.png , 2200
Android调用天气demo/WeatherDemo/bin/res/drawable/listview_line.png , 73
Android调用天气demo/WeatherDemo/bin/res/drawable/main_bg.png , 1290
Android调用天气demo/WeatherDemo/bin/res/drawable/s_1.png , 4938
Android调用天气demo/WeatherDemo/bin/res/drawable/s_10.png , 5413
Android调用天气demo/WeatherDemo/bin/res/drawable/s_11.png , 5268
Android调用天气demo/WeatherDemo/bin/res/drawable/s_12.png , 3371
Android调用天气demo/WeatherDemo/bin/res/drawable/s_13.png , 4403
Android调用天气demo/WeatherDemo/bin/res/drawable/s_2.png , 3269
Android调用天气demo/WeatherDemo/bin/res/drawable/s_3.png , 3992
Android调用天气demo/WeatherDemo/bin/res/drawable/s_4.png , 4619
Android调用天气demo/WeatherDemo/bin/res/drawable/s_5.png , 4611
Android调用天气demo/WeatherDemo/bin/res/drawable/s_6.png , 6294
Android调用天气demo/WeatherDemo/bin/res/drawable/s_7.png , 5590
Android调用天气demo/WeatherDemo/bin/res/drawable/s_8.png , 4356
Android调用天气demo/WeatherDemo/bin/res/drawable/s_9.png , 4967
Android调用天气demo/WeatherDemo/bin/res/drawable-hdpi/icon.png , 3966
Android调用天气demo/WeatherDemo/bin/res/drawable-ldpi/icon.png , 1537
Android调用天气demo/WeatherDemo/bin/res/drawable-mdpi/icon.png , 2200
Android调用天气demo/WeatherDemo/bin/resources.ap_ , 112731
Android调用天气demo/WeatherDemo/bin/WeatherDemo.apk , 920737
Android调用天气demo/WeatherDemo/gen/cn/hhs/activity/BuildConfig.java , 157
Android调用天气demo/WeatherDemo/gen/cn/hhs/activity/R.java , 2895
Android调用天气demo/WeatherDemo/libs/armeabi/libBMapApiEngine_v1_3_2.so , 1015200
Android调用天气demo/WeatherDemo/libs/baidumapapi.jar , 230675
Android调用天气demo/WeatherDemo/proguard.cfg , 1159
Android调用天气demo/WeatherDemo/project.properties , 563
Android调用天气demo/WeatherDemo/res/drawable/btn_normal_g.jpg , 18166
Android调用天气demo/WeatherDemo/res/drawable/btn_return_g.jpg , 12402
Android调用天气demo/WeatherDemo/res/drawable/index_bottom_g.jpg , 1394
Android调用天气demo/WeatherDemo/res/drawable/listview_line.png , 2812
Android调用天气demo/WeatherDemo/res/drawable/main_bg.png , 4969
Android调用天气demo/WeatherDemo/res/drawable/s_1.png , 6420
Android调用天气demo/WeatherDemo/res/drawable/s_10.png , 6147
Android调用天气demo/WeatherDemo/res/drawable/s_11.png , 6769
Android调用天气demo/WeatherDemo/res/drawable/s_12.png , 5006
Android调用天气demo/WeatherDemo/res/drawable/s_13.png , 5574
Android调用天气demo/WeatherDemo/res/drawable/s_2.png , 3928
Android调用天气demo/WeatherDemo/res/drawable/s_3.png , 4868
Android调用天气demo/WeatherDemo/res/drawable/s_4.png , 5741
Android调用天气demo/WeatherDemo/res/drawable/s_5.png , 5685
Android调用天气demo/WeatherDemo/res/drawable/s_6.png , 6994
Android调用天气demo/WeatherDemo/res/drawable/s_7.png , 6337
Android调用天气demo/WeatherDemo/res/drawable/s_8.png , 4719
Android调用天气demo/WeatherDemo/res/drawable/s_9.png , 5335
Android调用天气demo/WeatherDemo/res/drawable/top_bg_g_repe.xml , 199
Android调用天气demo/WeatherDemo/res/drawable-hdpi/icon.png , 4147
Android调用天气demo/WeatherDemo/res/drawable-ldpi/icon.png , 1723
Android调用天气demo/WeatherDemo/res/drawable-mdpi/icon.png , 2574
Android调用天气demo/WeatherDemo/res/layout/common_line.xml , 390
Android调用天气demo/WeatherDemo/res/layout/main.xml , 382
Android调用天气demo/WeatherDemo/res/layout/weather_other_city.xml , 528
Android调用天气demo/WeatherDemo/res/layout/weather_screen.xml , 10708
Android调用天气demo/WeatherDemo/res/values/strings.xml , 181
Android调用天气demo/WeatherDemo/src/cn/hhs/activity/WeatherScreen.java , 13505
Android调用天气demo/WeatherDemo/src/cn/hhs/util/DataUtil.java , 564
Android调用天气demo/WeatherDemo/src/cn/hhs/util/HttpService.java , 4218
没有账号? 忘记密码?

社交账号快速登录