Android 谷歌天气预报该项目是东方标准广州java培训中心为java技术开发学习人员和android技术开发班的学员共同开发研制的一款生活助手软件《谷歌天气预报》。
项目来自于日常生活中天气预报新闻。
由于人们日益紧凑的生活节奏,对于天气的关注不如以前那么重要,所以为了节省人们的业余时间,直接将天气预报功能搬入了手机软件功能当中,方便人们阅读浏览当地天气情况,并适当做出生活调整。
该项目技术要点为:推送功能+后台服务设计Service+联网功能HTTP+xml解析器XmlPullParser等技术。
一.项目介绍《谷歌天气预报》是一款简单的天气预报定制软件。
界面简洁易懂,方便用户使用。
可以查询几大城市的天气预报情况,主要功能有天气预告、城市温度、湿度、未来几天天气预测等。
该软件使用起来非常方便。
只要输入所查看的城市,软件自动提供当地的天气情况,以供查询。
二.For personal use only in study and research; not for commercial use三.四.功能说明1、进入软件页面2、功能说明1)、【天气】功能:显示当前天气情况。
资料来自于互联网真实天气预报。
2)、【城市】功能:显示当前所在城市。
3)、【温度】功能:显示选择城市天气温度变化。
资料来自互联网真实天气温度预告。
4)、【湿度】功能:显示当地城市湿度5)、【天气预测】功能:预测未来几天的天气状况。
可以预测未来三天以内的天气资料。
6)、【更新日期】功能: 显示当前天气日期。
五.程序设计技术说明本程序主要使用Service服务。
Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service。
Service 可以分为有无限生命和有限生命两种。
特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用 startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。
在本程序中除了使用Seivice服务外,还用到了网络xml解析器XmlPullParser。
同时还使用了监听器OnClickListener 来进行监听动作。
程序部分主要代码1:// 更新UI线程handler = new Handler();thread = new Thread() {@Overridepublic void run() {// 未完成ui更改if (!done) {// 加载完成if (WeatherService.finishLoad) {// 今天信息tv_temp.setText("温度: "++ "℃");tv_city.setText("城市: "+ ;tv_weather.setText("天气: "+ WeatherService.weather .getCurrent_condition());tv_humidity.setText(WeatherService.weather.getCurrent_humidity());tv_date.setText("更新日期: "++ " " + ;// 明天信息tv_2ndday.setText(WeatherService.weather.getForecast1_day_of_week());tv_2ndweather.setText(WeatherService.weather.getForecast1_condition());tv_2ndtem.setText(WeatherService.weather.getForecast1_low()+ "~"++ "℃");// 后天信息tv_3rdday.setText(WeatherService.weather.getForecast2_day_of_week());tv_3rdweather.setText(WeatherService.weather.getForecast2_condition());tv_3rdtem.setText(WeatherService.weather.getForecast2_low()+ "~"++ "℃");// 大后天信息tv_4thday.setText(WeatherService.weather.getForecast3_day_of_week());tv_4thweather.setText(WeatherService.weather.getForecast3_condition());tv_4thtem.setText(WeatherService.weather.getForecast3_low()+ "~"++ "℃");// 关闭服务;WeatherService.finishLoad = false;done = true;overtimeCounter = 0;} else {overtimeCounter++;// 超时关闭服务if (overtimeCounter >= 10) {Toast.makeText(WeatherActivity.this, "连接超时,请重新连接",1000).show();// 关闭服务;WeatherService.finishLoad = false;done = true;overtimeCounter = 0;}handler.postDelayed(thread, 500);}}}};}程序部分主要代码2:public class WeatherService extends Service {public static Weather weather;public static String city;public static boolean finishLoad = false;//forecast_conditions标签计数器public int forecastcounter;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {getWeather(city);}@Overridepublic void onStart(Intent intent, int startId) { }@Overridepublic void onDestroy() {weather = null;}private void getWeather(String city) {weather = null;try {// 定义pull解析器XmlPullParser pullParser =Xml.newPullParser();//统一资源定位器URL url = new URL("/api?hl=zh-cn&weather="+ city);//带缓冲的字符输出流BufferedReader bReader = new BufferedReader(new InputStreamReader(url.openStream(), "GBK"));StringBuilder sb = new StringBuilder();String temp;//写入到StringBuilderwhile ((temp = bReader.readLine()) != null) { sb.append(temp);}bReader.close();//去除xml开头的空格pullParser.setInput(newStringReader(sb.toString().trim()));int eventType = pullParser.getEventType();//判断是否在current_conditions标签内boolean isCurrent = false;// 未读到文件结束标签while (eventType !=XmlPullParser.END_DOCUMENT) {"eventType:" + eventType);switch (eventType) {case XmlPullParser.START_DOCUMENT://初始化天气对象weather = new Weather();break;//开始标签case XmlPullParser.START_TAG:String tagName = pullParser.getName();if (tagName.equals("city")) {//获取城市weather.setCity(pullParser.getAttributeValue(0));} else if(tagName.equals("forecast_date")) {//获取日期weather.setForecast_date(pullParser.getAttributeV alue(0));} else if(tagName.equals("current_conditions")) {//是否在current_conditions标签内isCurrent = true;} else if (isCurrent &&tagName.equals("condition")) {//获取天气weather.setCurrent_condition(pullParser.getAttrib uteValue(0));} else if (isCurrent &&tagName.equals("temp_c")) {//获取温度weather.setCurrent_temp_c(Integer.parseInt(pullPa rser.getAttributeValue(0)));} else if(isCurrent &&tagName.equals("humidity")){//获取湿度weather.setCurrent_humidity(pullParser.getAttribu teValue(0));//}elseif(tagName.equals("forecast_conditions")){forecastcounter++;//第一次进入forecast_conditions}else if(forecastcounter == 1 && tagName.equals("day_of_week")){//今天星期weather.setCurrent_day_of_week(pullParser.getAttr ibuteValue(0));//第二次进入forecast_conditions}else if(forecastcounter == 2 && tagName.equals("day_of_week")){//明天星期weather.setForecast1_day_of_week(pullParser.getAt tributeValue(0));}else if(forecastcounter == 2 && tagName.equals("low")){//明天最低温weather.setForecast1_low(Integer.parseInt(pullPar ser.getAttributeValue(0)));}else if(forecastcounter == 2 && tagName.equals("high")){//明天最高温weather.setForecast1_high(Integer.parseInt(pullPa rser.getAttributeValue(0)));}else if(forecastcounter == 2 && tagName.equals("condition")){//明天天气weather.setForecast1_condition(pullParser.getAttr ibuteValue(0));//第三次进入forecast_conditions}else if(forecastcounter == 3 && tagName.equals("day_of_week")){//后天星期weather.setForecast2_day_of_week(pullParser.getAt tributeValue(0));}else if(forecastcounter == 3 && tagName.equals("low")){//后天最低温weather.setForecast2_low(Integer.parseInt(pullPar ser.getAttributeValue(0)));}else if(forecastcounter == 3 && tagName.equals("high")){//后天最高温weather.setForecast2_high(Integer.parseInt(pullPa rser.getAttributeValue(0)));}else if(forecastcounter == 3 && tagName.equals("condition")){//后天天气weather.setForecast2_condition(pullParser.getAttr ibuteValue(0));//第四次进入forecast_conditions}else if(forecastcounter == 4 && tagName.equals("day_of_week")){//大后天星期weather.setForecast3_day_of_week(pullParser.getAt tributeValue(0));}else if(forecastcounter == 4 && tagName.equals("low")){//大后天最低温weather.setForecast3_low(Integer.parseInt(pullPar ser.getAttributeValue(0)));}else if(forecastcounter == 4 && tagName.equals("high")){//大后天最高温weather.setForecast3_high(Integer.parseInt(pullPa rser.getAttributeValue(0)));}else if(forecastcounter == 4 && tagName.equals("condition")){//大后天天气weather.setForecast3_condition(pullParser.getAttr ibuteValue(0));}break;case XmlPullParser.END_TAG:tagName = pullParser.getName();//读取到current_conditions的结束标签if(tagName.equals("current_conditions")){isCurrent = false;}break;case XmlPullParser.END_DOCUMENT:break;default:break;}eventType = pullParser.next();}finishLoad = true;} catch (MalformedURLException e) { // TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) { // TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}广州东方标准职业教育培训中心网址:电话:地址:广州市天河区五山路华南理工大学科技园金华园C112广州市海珠区新港西路135号中山大学科技综合楼A座1503室仅供个人用于学习、研究;不得用于商业用途。