Android Open Source - Weather Update Widget Service






From Project

Back to project page Weather.

License

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.

Java Source Code

package com.imlongluo.weather.app;
/*from w ww. j a  v a2s. c o m*/
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

/**  
 * 
 * @author longluo
 * ??????????widget??????????????
 */
public class UpdateWidgetService extends Service {

  //?????????
  AlarmManager alarm;
  PendingIntent pintent;
  
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override //??????????widget??????
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    
    Log.i("widget Service", "===================update  widget===========================");
    //??widget?????
    RemoteViews views = WeatherWidget.getWeatherView(this);
    //??AppWidgetManager widget?????
    AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this);
    
    int[] appids=appWidgetManager.getAppWidgetIds(new ComponentName(this, WeatherWidget.class));
    
    //======================================================
    //??????
    SharedPreferences sp=getSharedPreferences(SetCityActivity.CITY_CODE_FILE, SetCityActivity.MODE_PRIVATE);
    String cityCode= sp.getString("code", "");
    if(cityCode!=null&&cityCode.trim().length() > 0) {
      Log.i("widget", "===================update  weather===========================");
      WeatherWidget.updateAppWidget(views, this, appWidgetManager, cityCode);
    }
    //======================================================
    
    
    appWidgetManager.updateAppWidget(appids, views);
    
    //?????????
    Date date = new Date();
    long now =date.getTime();
    long unit=60000;//?????
    int s=date.getSeconds();  //????
    unit=60000-s*1000;        //???????
    
    pintent=PendingIntent.getService(this, 0, intent, 0);
    
    //???
    alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    //AlarmManager.RTC_WAKEUP???????????????????
    //???????????????service??
    alarm.set(AlarmManager.RTC_WAKEUP, now+unit, pintent);
  }

  @Override //?widget?????context.stopService??????????service?????
  public void onDestroy() {
    
    //??????????
    if(alarm!=null) {
      alarm.cancel(pintent);
    }
    super.onDestroy();
  }
}




Java Source Code List

.WebAccessTools.java
com.imlongluo.weather.app.MainActivity.java
com.imlongluo.weather.app.SetCityActivity.java
com.imlongluo.weather.app.UpdateWidgetService.java
com.imlongluo.weather.app.WeatherWidget.java
com.imlongluo.weather.db.DBHelper.java
com.imlongluo.weather.location.GPSListAdapter.java
com.imlongluo.weather.location.MyListAdapter.java
com.imlongluo.weather.utils.LocationXMLParser.java
com.imlongluo.weather.utils.WeaterInfoParser.java