Back to project page Weather.
The source code is released under:
GNU General Public License
If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.imlongluo.weather.app; /*w w w . ja va 2s . com*/ import java.text.SimpleDateFormat; import java.util.Date; import org.json.JSONException; import org.json.JSONObject; import com.imlongluo.weather.R; import com.imlongluo.weather.utils.WebAccessTools; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; import android.widget.RemoteViews; public class WeatherWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); //???????????????????????? getWeatherView(context); //???????????widget?????????? context.startService(new Intent(context,UpdateWidgetService.class)); } @Override //?????????Widget??????? public void onDisabled(Context context) { super.onDisabled(context); //??????????? context.stopService(new Intent(context,UpdateWidgetService.class)); } //??widget???????? public static RemoteViews getWeatherView(Context context){ RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.widget_layout); //??widget??????????MainActivity?????????????????? Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.weather_rootLayout, pendingIntent); return views; } public static void updateAppWidget(RemoteViews views, Context context, AppWidgetManager appWidgetManager, String cityCode) { SharedPreferences shared = context.getSharedPreferences(MainActivity.STORE_WEATHER, MainActivity.MODE_PRIVATE); long currentTime = System.currentTimeMillis(); //????????????? long vaildTime = shared.getLong("validTime", currentTime); //????????????????????????????? if(vaildTime <= currentTime) updateWeather(views, context, cityCode); else updateWeather(views, context); //???? Date date = new Date(); SimpleDateFormat foramt = new SimpleDateFormat("hh:mm"); String timeText = foramt.format(date); Log.i("widget", "===================update time======"+timeText+"====================="); views.setTextViewText(R.id.widget_time , timeText); } //???????????????? public static void updateWeather(RemoteViews views, Context context) { SharedPreferences sp = context.getSharedPreferences(MainActivity.STORE_WEATHER,MainActivity.MODE_PRIVATE); String info=sp.getString("city", ""); views.setTextViewText(R.id.widget_city, info); info=sp.getString("date_y", ""); views.setTextViewText(R.id.widget_data01, info); info= sp.getString("temp1", ""); views.setTextViewText(R.id.widget_temp, info); info= sp.getString("weather1", ""); views.setTextViewText(R.id.widget_weather, info); views.setImageViewResource(R.id.widget_icon, sp.getInt("img_title1", R.drawable.weathericon_condition_17)); } //???????????views???????? public static void updateWeather(RemoteViews views, Context context, String cityCode) { //????????? StringBuffer str = new StringBuffer("http://m.weather.com.cn/data/"); str.append(cityCode); str.append(".html"); try { String info =new WebAccessTools(context).getWebContent(str.toString()); JSONObject json=new JSONObject(info).getJSONObject("weatherinfo"); int weather_icon = 0; //??????????? SharedPreferences.Editor editor = context.getSharedPreferences(MainActivity.STORE_WEATHER, MainActivity.MODE_PRIVATE).edit(); //???? info=json.getString("city"); editor.putString("city", info); views.setTextViewText(R.id.widget_city, info); //?????? info= json.getString("date_y") ; info= info+"("+json.getString("week")+")"; editor.putString("date_y", info); views.setTextViewText(R.id.widget_data01, info); //???? info= json.getString("date"); editor.putString("date", info); //???? info= json.getString("temp1"); editor.putString("temp1", info); views.setTextViewText(R.id.widget_temp, info); //???? info= json.getString("weather1"); editor.putString("weather1", info); views.setTextViewText(R.id.widget_weather, info); //???? info= json.getString("img_title1"); weather_icon = MainActivity.getWeatherBitMapResource(info); editor.putInt("img_title1", weather_icon); views.setImageViewResource(R.id.widget_icon, weather_icon); //?????? info= json.getString("wind1"); editor.putString("wind1", info); //???? info= json.getString("index_d"); editor.putString("index_d", info); //??????? info= json.getString("weather2"); editor.putString("weather2", info); //????? info= json.getString("img_title2"); weather_icon = MainActivity.getWeatherBitMapResource(info); editor.putInt("img_title2", weather_icon); //????? info= json.getString("temp2"); editor.putString("temp2", info); //????? info= json.getString("wind2"); editor.putString("wind2", info); //??????? info= json.getString("weather3"); editor.putString("weather3", info); //???????? info= json.getString("img_title3"); weather_icon = MainActivity.getWeatherBitMapResource(info); editor.putInt("img_title3", weather_icon); //??????? info= json.getString("temp3"); editor.putString("temp3", info); //??????? info= json.getString("wind3"); editor.putString("wind3", info); //?????????5??? long validTime = System.currentTimeMillis(); validTime = validTime + 5*60*60*1000; editor.putLong("validTime", validTime); //??? editor.commit(); }catch(JSONException e) { e.printStackTrace(); } } }