protected void onResume() { if (mWeatherContainer.getChildCount() != 0) { mWeatherContainer.removeAllViews(); } String temperature = config.getShowType() > 0 ? "f" : "c"; // showWeatherInfos("2161853", temperature); showWeatherInfos(temperature); handler.sendEmptyMessageDelayed(WEATHER_IS_READY, config.getUpdateFrequency() * 60 * 60 * 1000); }
@Override public void handleMessage(Message msg) { if (msg.what == WEATHER_IS_READY) { // 利用handler发送延时消息,根据用户指定更新时间,更新天气 if (mWeatherContainer.getChildCount() != 0) { mWeatherContainer.removeAllViews(); } // String cityCode = config.getCityCode(); String temperature = config.getShowType() > 0 ? "f" : "c"; // showWeatherInfos("2161853", temperature); showWeatherInfos(temperature); handler.sendEmptyMessageDelayed( WEATHER_IS_READY, config.getUpdateFrequency() * 60 * 60); } else if (msg.what == GET_WEATHER_SUCCESS) { List<WeatherInfo> infos; SAXParserFactory factory = null; SAXParser saxParser = null; XMLReader xmlReader = null; GetYahooWeatherSaxTools tools = null; try { factory = SAXParserFactory.newInstance(); saxParser = factory.newSAXParser(); xmlReader = saxParser.getXMLReader(); infos = new ArrayList<WeatherInfo>(); tools = new GetYahooWeatherSaxTools(infos); xmlReader.setContentHandler(tools); xmlReader.parse(new InputSource(new StringReader((String) msg.obj))); // 获取当前是周几,用数字表示 int currentDay = mWeekList.indexOf(infos.get(0).getDay()); int forecastDay = 0; // 将得到的list天气数据,展示到屏幕上 for (int i = 0; i < config.getForecastDays(); i++) { view = View.inflate(context, R.layout.weather_item, null); TextView tvDay = (TextView) view.findViewById(R.id.tv_day); TextView tvLow = (TextView) view.findViewById(R.id.tv_temperature_low); TextView tvHigh = (TextView) view.findViewById(R.id.tv_temperature_high); TextView tvWeather = (TextView) view.findViewById(R.id.tv_weather); ImageView ivWeatherIcon = (ImageView) view.findViewById(R.id.iv_weather_icon); forecastDay = currentDay + i; if (forecastDay >= 7) { forecastDay = forecastDay % 7; } tvDay.setText( context.getResources() .getStringArray(R.array.weather_day_normalform)[forecastDay]); tvLow.setText(infos.get(i).getLow() + mShowTypeOfTemperature[config.getShowType()]); tvHigh.setText( infos.get(i).getHigh() + mShowTypeOfTemperature[config.getShowType()]); tvWeather.setText( context.getResources() .getStringArray(R.array.weather_condition)[infos.get(i).getCode()]); ivWeatherIcon.setImageResource(R.drawable.w0); int width = config.getWidth() / config.getForecastDays(); int height = config.getHeight(); float wScale = width / 100f; float hScale = height / 100f; float squarScale = width * height / (100 * 100); LayoutParams viewChildParams = (LayoutParams) tvDay.getLayoutParams(); viewChildParams.height *= hScale; viewChildParams.width *= wScale; tvDay.setLayoutParams(viewChildParams); viewChildParams = (LayoutParams) tvLow.getLayoutParams(); viewChildParams.height *= hScale; viewChildParams.width *= wScale; tvLow.setLayoutParams(viewChildParams); tvHigh.setLayoutParams(viewChildParams); viewChildParams = (LayoutParams) tvWeather.getLayoutParams(); viewChildParams.height *= hScale; viewChildParams.width *= wScale; tvWeather.setLayoutParams(viewChildParams); viewChildParams = (LayoutParams) ivWeatherIcon.getLayoutParams(); viewChildParams.height *= hScale; viewChildParams.width *= wScale; ivWeatherIcon.setLayoutParams(viewChildParams); tvDay.setTextSize(DensityUtil.dip2px(context, 14 * squarScale)); tvLow.setTextSize(DensityUtil.dip2px(context, 14 * squarScale)); tvHigh.setTextSize(DensityUtil.dip2px(context, 14 * squarScale)); tvWeather.setTextSize(DensityUtil.dip2px(context, 14 * squarScale)); LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); viewParams.weight = 1; mWeatherContainer.addView(view, viewParams); } } catch (Exception e) { e.printStackTrace(); } } super.handleMessage(msg); }