public void locationFailure(final Context context, boolean active_fresh) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weather_widget); updateViews(context, views); ComponentName provider = new ComponentName(context, WeatherWidget.class); AppWidgetManager.getInstance(context).updateAppWidget(provider, views); if (active_fresh) { Intent i = new Intent(); i.setClassName( "com.morncloud.weatherwidget", "com.morncloud.weatherwidget.activity.WidgetDialogActivity"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(i); } else { String localCity = DatabaseHelper.getLocalCity(context); if (TextUtils.isEmpty(localCity)) { Intent i = new Intent(); i.setClassName( "com.morncloud.weatherwidget", "com.morncloud.weatherwidget.activity.WidgetDialogActivity"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } }
private void updateWeatherViews(Context context, RemoteViews views) { // 先查询当前城市 String localCity = DatabaseHelper.getLocalCity(context); // 再通过contentprovider获取当前城市的信息 WeatherInfo weatherInfo = DatabaseHelper.getTodayData( context, localCity); // WeatherResolver.getNowData(context, "北京"); if (weatherInfo != null) { String weather; if (DateUtil.isNight()) { weather = weatherInfo.getValueByKey(WeatherInfo.WEATHER_NIGHT); if (TextUtils.isEmpty(weather)) { weather = weatherInfo.getValueByKey(WeatherInfo.WEATHER_DAY); } } else { weather = weatherInfo.getValueByKey(WeatherInfo.WEATHER_DAY); } String maxtemper = weatherInfo.getValueByKey(WeatherInfo.MAXTEMPER); String mintemper = weatherInfo.getValueByKey(WeatherInfo.MINTEMPER); String temper = mintemper + "\b~\b" + maxtemper + context.getResources().getString(R.string.temperature_sign); views.setTextViewText(R.id.tv_widget_temper, temper); views.setImageViewResource( R.id.iv_weather, ResourceUtil.getResourceByWeather(weather, true, true)); if (!TextUtils.isEmpty(weather) && weather.length() > 5) { weather = weather.substring(0, 4) + ".."; } views.setTextViewText(R.id.tv_widget_weather, weather); String city = weatherInfo.getValueByKey(WeatherInfo.CITY_NAME); views.setTextViewText( R.id.tv_location_city, city.substring(city.indexOf("|") + 1, city.length())); views.setViewVisibility(R.id.rl_weather, View.VISIBLE); views.setViewVisibility(R.id.rl_fresh_weather, View.INVISIBLE); } }
public void freshClick(final Context context) { String last_update = DatabaseHelper.getUpdateTime(context); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date; try { date = dateFormat.parse(last_update); long time = date.getTime(); if (System.currentTimeMillis() - time > WeatherConstant.UPDATE_FREQUENCY_TIME) { // 天气信息过期 clickOperation(context); } else { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weather_widget); updateViews(context, views); ComponentName provider = new ComponentName(context, WeatherWidget.class); AppWidgetManager.getInstance(context).updateAppWidget(provider, views); } } catch (Exception e) { clickOperation(context); } }